Am I using list comprehensions wrong? - python

So my Python code to keep on finding the sum of a number's digits (9983 = 9+9+8+3 = 29 = 2+9 = 11 = 1+1 = 2) below isn't working, and it gives me this exception
ValueError: invalid literal for int() with base 10: '['".
It was working before when instead of using list comprehension, I did it by iterating through the list normally, so I suspect it's a problem with the list comprehension? Help?
x = int(input())
def rec(x):
if (x<10):
return x
return rec(sum(x))
def sum(x):
lst = [int(y) for y in list(str(x))]
return sum(lst)
print(rec(x))

There's a built-in function called sum, and you defined a function with the same name - but you need the original function as part of the solution.
Let's start by renaming your version of sum to summation, and let's take the opportunity to simplify the code a bit more:
def rec(x):
if x < 10:
return x
return rec(summation(x))
def summation(x):
return sum(int(y) for y in str(x))
See how the built-in sum was needed inside summation? that's why your function must have a different name! And although we could have used a list comprehension, it's easier and more efficient to pass a generator expression to sum. Alternatively (but less efficiently) we could have used a list comprehension:
def summation(x):
lst = [int(y) for y in str(x)]
return sum(lst)
Either way, it works as expected:
rec(9983)
=>2

You are creating confusion for Python ;-)
The problem is in def sum(x):, not in the list comprehension. sum is a reserved keyword. Just rename the function (and the call to the function) and it works.

Related

list to integer

I'm trying to write a recursive python function that takes in a list for example [1,2,3,4] and returns an integer 1234. Any help on how to do this
def listtoint(lst):
if lst==[]:
return 0
return lst[-1:]+clti(lst/10)
I know you can't divide the list but I would like a way to get around it
def listtoint(lst):
if lst == []:
return 0
s = ''.join([str(i) for i in lst])
return int(s)
How this works is: ''.join(some_list) takes every element of the list and concatenates them into one long string. every element of some_list here must already be a string, thus the list comprehension in the code above.
int is then used to turn the resulting string into an integer.
There should be error checking but you can deal with that. Also, this isn't recursive and doesn't need to be.
To do this recursively...
def listtoint(lst):
if lst==[]:
return 0
iPower = 10**(len(lst)-1)
return lst[0]*iPower + listtoint(lst[1:])

Print a list of values from these functions

Hello so I was wondering how can I 'print' the list of numbers generated by these functions that take the values from the list and squares them. Thanks!!!
def square(list):
return [i ** 2 for i in list]
def square(list):
return map(lambda x: x ** 2, list)
def square(list):
for i in list:
yield i ** 2
def square(list):
ret = []
for i in list:
ret.append(i ** 2)
return ret
Printing a list by using print, for example:
print(', '.join(map(str, square([5,3,2]))
First off, you need different function names, or else when you call it, python will only take the last one.
To answer your question, it is as easy as using a print statement. Simply print the return of the function while using a list as the argument.
square_list = [1,2,3]
print square(square_list)
If you wanted to try a different way, putting the print statement in the function also works.
For example:
def square(list):
print [i ** 2 for i in list] # Instead of return
The downside of this is you cannot store it as a variable or append it to a list later on.
Happy coding!

Writing filter() function but getting typeError

my code consists of me recreating the function 'filter()' and using it with a function to filter words longer than 5 characters. It worked with the actual function filter when I tried it btw...I'm using python 3+
def filter1(fn, a):
i = 0
while i != len(a):
u = i - 1
a[i] = fn(a[i], a[u])
i += 1
return a
def filter_long_words(l):
if len[l] > 5:
return [l]
listered = ['blue', 'hdfdhsf', 'dsfjbdsf', 'jole']
print(list(filter1(filter_long_words, listered)))
getting error
TypeError: filter_long_words() takes 1 positional argument but 2 were given
You are passing two parameters to fn (which refers to filter_long_words) here:
a[i] = fn(a[i], a[u])
But filter_long_words only accepts one parameter.
Notes:
You can loop through lists using for item in my_list, or if you want index as well for index, item in enumerate(my_list).
I think you might get an IndexError since u will be -1 in the first round of your loop.
The filter function can also be expressed as a list comprehension: (item for item in listered if filter_long_words(item))
My version of filter would look like this, if I have to use a for loop:
def my_filter(fn, sequence):
if fn is None:
fn = lambda x: x
for item in sequence:
if fn(item):
yield item
Since you have stated that you are using Python 3, this returns a generator instead of a list. If you want it to return a list:
def my_filter(fn, sequence):
if fn is None:
fn = lambda x: x
acc = []
for item in sequence:
if fn(item):
acc.append(item)
return acc
If you don't need to use a for loop:
def my_filter(fn, sequence):
if fn is None:
fn = lambda x: x
return (item for item in sequence if fn(item))
Your're calling fn with 2 parameters in filter1(fn, a), and since you've passed filter_long_words() to filter1 as fn, that triggers the error.
But there's more weird stuff:
I don't understand the magick of filter1 or what you were trying to
accomplish, but it seems to me that you don't have a clear idea what to do.
But if you want to mimic (somehow) how filter works, you have to return a
list which contains only items for which the fn function returns true. When
you know this, you can rewrite it - here are a few suggestions for rewrite
# explicit, inefficient and long, but straightforward version:
def filter1(fn, a):
new_list = []
for item in a:
if fn(item):
new_list.append(item):
return new_list
# shorter version using list comprehensions:
def filter1(fn, a):
return [item for item in a if fn(item)]
The filter_long_words function is wrong too - it should return True or
False. The only reason why it could work is because any non-empty list is
treated as True by python and default return value of a function is None,
which translates to False. But it's confusing and syntactically wrong to use
len[l] - the proper usage is len(l).
There are a few suggestions for rewrite, which all returns explicit boolean
values:
# unnecessary long, but self-explanatory:
def filter_long_words(l):
if len(l) > 5:
return True
else
return False
# short variant
def filter_long_words(l):
return len(l) > 5
You are calling "filter_long_words" with 2 parameter => fn(a[i], a[u]) also there is an error
def filter_long_words(l):
if **len[l]** > 5:
return [l]
len is builtin method it should be len(l)

Runtime error, sorting function without using .sort

I am supposed to write the sorting function. Define
a function sort list that takes in a list and returns a list sorted in ascending order.
(4 marks)
Note: your code should make use of the insert list function defined earlier.
With my previous defined variables:
def search(x , seq):
for i in seq:
if x > max(seq):
return len(seq)
elif x < min(seq):
return 0
return search(x, seq[:-1])
def insert_tup(x, tup):
searchtuple = search(x, tup)
ontheleft = tup[:searchtuple]
ontheright = tup[ searchtuple:]
altogether = ontheleft + (x,) + ontheright
return altogether
def insert_list(x, lst):
A = search(x, lst)
lst.insert(A, x)
return lst
I did a trial
def sort_list(lst):
result = []
for i in lst:
result += [i]
return insert_list(result[0], lst)
But i keep getting Runtime error where
File "", line 7, in search
return search(x, seq[:-1])
The output is desired to be
sort_list([5, 1, 9])
[1, 5, 9]
You are recursively calling the search() method and never breaking out of this loop. I expect this is raising a RuntimeError: maximum recursion depth exceeded exception.
Python has a recursion limit to protect against infinite recursion. FYI you can modify the recursion limit via sys.setrecursionlimit - but this is not something you should be doing.
Instead you can fix this by re-writing your algorithm iteratively.
I also suggest you read the Python sorting wiki, as this demonstrates the pythonic way to sort things with the built in sorted() function and the list method sort(). You don't need to re-invent the wheel.
You need a base case for the recursion. Something like:
if len(seq)==1:
return seq
Otherwise it calls itself forever. And the result is the error that you see.

(Python 2.7) Use a list as an argument in a function?

So I'm trying to learn Python using codecademy but I'm stuck. It's asking me to define a function that takes a list as an argument. This is the code I have:
# Write your function below!
def fizz_count(*x):
count = 0
for x in fizz_count:
if x == "fizz":
count += 1
return count
It's probably something stupid I've done wrong, but it keeps telling me to make sure the function only takes one parameter, "x". def fizz_count(x): doesn't work either though. What am I supposed to do here?
Edit: Thanks for the help everyone, I see what I was doing wrong now.
There are a handful of problems here:
You're trying to iterate over fizz_count. But fizz_count is your function. x is your passed-in argument. So it should be for x in x: (but see #3).
You're accepting one argument with *x. The * causes x to be a tuple of all arguments. If you only pass one, a list, then the list is x[0] and items of the list are x[0][0], x[0][1] and so on. Easier to just accept x.
You're using your argument, x, as the placeholder for items in your list when you iterate over it, which means after the loop, x no longer refers to the passed-in list, but to the last item of it. This would actually work in this case because you don't use x afterward, but for clarity it's better to use a different variable name.
Some of your variable names could be more descriptive.
Putting these together we get something like this:
def fizz_count(sequence):
count = 0
for item in sequence:
if item == "fizz":
count += 1
return count
I assume you're taking the long way 'round for learning porpoises, which don't swim so fast. A better way to write this might be:
def fizz_count(sequence):
return sum(item == "fizz" for item in sequence)
But in fact list has a count() method, as does tuple, so if you know for sure that your argument is a list or tuple (and not some other kind of sequence), you can just do:
def fizz_count(sequence):
return sequence.count("fizz")
In fact, that's so simple, you hardly need to write a function for it!
when you pass *x to a function, then x is a list. Do either
def function(x):
# x is a variable
...
function('foo') # pass a single variable
funciton(['foo', 'bar']) # pass a list, explicitly
or
def function(*args):
# args is a list of unspecified size
...
function('foo') # x is list of 1 element
function('foo', 'bar') # x is list with two elements
Your function isn't taking a list as an argument. *x expands to consume your passed arguments, so your function is expecting to be called like this:
f(1, 2, 3)
Not like this:
f([1, 2, 3])
Notice the lack of a list object in your first example. Get rid of the *, as you don't need it:
# Write your function below!
def fizz_count(lst):
count = 0
for elem in lst:
if elem == "fizz":
count += 1
return count
You can also just use list.count:
# Write your function below!
def fizz_count(lst):
return lst.count('fizz')
It must be a typo. You're trying to iterate over the function name.
try this:
def fizz_count(x):
counter = 0
for element in x:
if element == "fizz":
counter += 1
return counter
Try this:
# Write your function below!
def fizz_count(x):
count = 0
for i in x:
if i == "fizz":
count += 1
return count
Sample :
>>> fizz_count(['test','fizz','buzz'])
1
for i in x: will iterate through every elements of list x. Suggest you to read more here.

Categories