Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: can only concatenate list (not "int") to list in python

Tags:

python

I tried to run this code, but it showed an error:

def shoot(aliens):

    s=[0]*1000
    s[0]=0
    s[1]=1
    num=len(aliens)
    b=[[0 for m in range(1000)] for n in range(1000)]
    for j in xrange(2,num):
        for i in xrange(0,j):

                b[j][i]=s[i]+min(int(aliens[j]),f[j-i]) ##Error here
        s[j]=max(b)

and the error:

Traceback (most recent call last):
File "module1.py", line 67, in <module>
print shoot(line)
File "module1.py", line 26, in shoot
b[j][i]=s[i]+min(int(aliens[j]),f[j-i])
TypeError: can only concatenate list (not "int") to list

please help!

Edit: added more code. s, aliens and f are other arrays. I tried to save the result to the 2 dimentional array, but it showed that error.

like image 730
Tung Pham Avatar asked Jul 19 '13 20:07

Tung Pham


1 Answers

s[j] = max(b)

doesn't treat b as a 2-d array of integers and pick the biggest one. b is a list of lists. max(b) compares the lists and returns the one that compares highest. (List comparison is done by comparing the elements lexicographically.)

You want

s[j] = max(max(sublist) for sublist in b)
like image 124
user2357112 supports Monica Avatar answered Sep 28 '22 06:09

user2357112 supports Monica