This question already has answers here:
calculate length of list recursively [duplicate]
(2 answers)
Closed 7 years ago.
So, my goal is to count the number of arguments in a list using recursion. However, as I'm only to use one argument in the function call, I don't know how to solve it without a second "count" argument.
So far I have this, where I accumulate the 1's together.
def countElements(a):
if a==[]:
return []
else:
return [1] + countElements(a[1:])
def main():
a=[3,2,5,3]
print(countElements(a))
main()
Instead of returning an empty list in the base case, return 0, and instead of [1] + countElements(a[1:]), return 1 + countElements(a[1:]). That way, you're just keeping the running count instead of getting a list back.
Related
This question already has answers here:
How can I use `return` to get back multiple values from a loop? Can I put them in a list?
(2 answers)
Closed 7 months ago.
this function is supposed to return the list in reverse but instead, it is only returning the last element of the list. I would like to know what is the issue.
def reverse_list(lt):
for f in lt[::-1]:
return f
lt = [1,2,3,4]
print(reverse_list(lt))
def reverse_list(lt):
return lt[::-1]
lt = [1,2,3,4]
print(reverse_list(lt))
This question already has answers here:
Apply function to each element of a list
(4 answers)
Appending to a list comprehension in Python returns None
(3 answers)
Closed 1 year ago.
I am trying to minimize as much as possible my coding and trying to solve problems in different ways and i wrote this code which gets a list of numbers and it returns the square root of each one number in list
def square_roots(l):
squareRoots = []
result = [squareRoots.append(math.sqrt(i)) for i in l]
return result
l=[1,2,3]
print(square_roots(l))
The only problem is that it returns [None, None, None] instead of the square root of each number in the array.
What's wrong with it?
list.append() (i.e. squareRoots.append()) returns None. Remove that part.
def square_roots(l):
result = [math.sqrt(i) for i in l]
return result
You might want to read Why does append() always return None in Python?
This question already has answers here:
Finding median of list in Python
(28 answers)
Closed 3 years ago.
I have a problem that one of the things i need to include is a subscript of median, but i have no clue what that means
I have tried most things but again i have no clue what subscript of median means.
def median(a):
a=a.sort()
a=len(a)/2
return a
def main():
print(median([3,1,2]))
print(median([4,3,2,1]))
print(median([1,5,3,2,4]))
print(median([6,5,1,2,3,4]))
main()
I expect it to print out the median of the numbers if it gets two i need the lesser... We cant use average.
You're returning the middle index, not the value of the element at that index.
Also, a.sort() modifies the list in place, it doesn't return the sorted list; a = a.sort() sorts the list and then sets a to None.
def median(a):
s = sorted(a)
middle = int(len(s)/2)
return s[middle]
This question already has answers here:
Why is "None" printed after my function's output?
(7 answers)
Closed 5 years ago.
I'm trying to create a function that uses a while loop to count up from one to a number given by a user. The code executes as I intend it to but returns None at the end. How do I get rid of the None? Here's the code.
def printFunction(n):
i = 1
while i <= n:
print(i)
i+=1
print (printFunction(int(input())))
You can use this code to prevent none, tough its just the last line changed
def printFunction(n):
i = 1
while i <= n:
print(i)
i+=1
printFunction(int(input()))
In the last line you were using
print(printFunction(int(input()))) which was getting you None after printing the results.
Instead just use printFunction(int(input())). This will not print None. You can also use a message to ask user like printFunction(int(input("Enter a number"))). Since there is noting getting returned you no need to use print.
This question already has answers here:
Find the index of the second occurrence of a string inside a list
(3 answers)
Find the index of the n'th item in a list
(11 answers)
Closed 7 years ago.
If I'm working with a list containing duplicates and I want to know the index of a given occurrence of an element but I don't know how many occurrences of that element are in the list, how do I avoid calling the wrong occurrence?
Thanks
I don't know that a single builtin does this thing alone, but you could fairly easily write it, for instance:
def index_second_occurence(alist, athing):
if alist.count(athing) > 1:
first = alist.index(athing)
second = alist[first + 1::].index(athing)
return second + first + 1
else:
return - 1