Reputation: 21
The problem is:
Given a "two-dimensional list" of integers – that is, a list where each
value in it is a list of integers – find all the even numbers and sum them together.
The first one is my first attempt where I seem to have successfully made it work with 1 list. Then I try to make it work with a 2D list, and this code gives me an error 'int object is not subscriptible' and I think it's to do with my while statement. Any help would be appreciated, thanks!
def sum_even(xss):
result = 0
for item in xss:
if item % 2 == 0:
result = result + item
return result
ans = sum_even([4,8])
print (ans)
def sum_even(xss):
result = 0
n = 0
i = 0
j = 0
while xss[i][0] in xss:
if xss[i][0] % 2 == 0:
result = result + xss [i][0]
while xss[0][j] in xss:
if xss[0][j] % 2 == 0:
result = result +xss[0][j]
i +=1
return result
ans = sum_even([4,8])
print (ans)
Upvotes: 2
Views: 1599
Reputation: 73450
There are multiple issues with your code. The simplest would be using
sum
with a nested conditional generator expression:
def sum_even(xss):
return sum(x for sub_lst in xss for x in sub_lst if not x % 2)
The scopes of the nested for-expressions seem counter-intuitive at times. This is roughly equivalent to:
def sum_even(xss):
result = 0
for sub_lst in xss:
for x in sub_lst:
if not x % 2:
result += x
return result
For arbitrarily nested lists of integers, you would have to use recursion:
def sum_even(xss):
if isinstance(xss, int):
return (not xss % 2) * xss # the bool expr is neatly coerced to 1 or 0 ;)
return sum(sum_even(sub) for sub in xss)
Upvotes: 4
Reputation: 2327
>>> a=[[1,2],[3,4],[5,6]]
>>> reduce(lambda s,x:s+x,[i for l in a for i in l if not i%2],0)
12
without itertools and using just list comprehension.
Much simpler thanks to @juanpa.arrivillaga, much simpler version
>>> a=[[1,2],[3,4],[5,6,7,8]]
>>> sum([i for l in a for i in l if not i%2])
20
Upvotes: 1
Reputation: 13
Specifically this error means you are trying to use an int
as if it were a list
.
The cause for this is that your second sum_even
function was built to handle 2D lists, but you are using it on a 1D list ([4,8]
) instead of on a 2D list ([[4],[8]]
).
Despite of that, your code has another error:
def sum_even(xss):
result = 0
n = 0
i = 0
j = 0
while xss[i][0] in xss:
if xss[i][0] % 2 == 0:
result = result + xss [i][0]
while xss[0][j] in xss:
if xss[0][j] % 2 == 0:
result = result +xss[0][j]
i +=1
#there is no update for j
return result
In python, for statements dont use indexes with the iterator. The result is something more like a phrase:
For each 1d_list
within the 2D list xss
, check the elements
. If the element
is even, add it to result
.
def sum_even(xss):
result = 0
for 1d_list in xss:
for element in 1d_list:
if element%2 == 0:
result = result + element
return result
In this way you dont make any assumption on the length of the lists (each 1D sublist may have a different size).
Upvotes: 1
Reputation: 6748
You could try this. It works with any dimension list (not just 2):
list2d=[[1,2],[2,3,4]]
result=sum([int(elem) for elem in str(list2d).replace('[','').replace(']','').split(',') if not int(elem)%2])
Upvotes: 1
Reputation: 71451
You can try this:
import itertools
even_numbers = sum(i for i in itertools.chain.from_iterable(first_list) if i%2 == 0)
Upvotes: 2