14

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.

3
  • 6
    what are s, f and aliens? Commented Jul 19, 2013 at 20:50
  • 1
    can you please explain what your trying to get at the end ? Commented Jul 19, 2013 at 20:51
  • 1
    well, this is a dynamic programming algorithm of shooting the maximum aliens coming to the base. Commented Jul 19, 2013 at 21:04

4 Answers 4

10
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)
Sign up to request clarification or add additional context in comments.

1 Comment

oh. it seems to be another error if it goes through that error above. thank you to pointing it out.
1

try:

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[j][i] + min(int(aliens[j]),f[j-i])

It seems to me likes is a 2D list (list of a list), and thus, you can't perform the operation.

s[j] + min(int(aliens[j]),f[j-i])

4 Comments

it is working, but is there any other way to do that because I do not want to change 1D array to 2D array.
s is not a list of lists.
@user2357112, if it is not a list of list, what is it then?
s=[0]*1000 initializes s to a list of ints. The bugged s[j]=max(b) sets s[2] to a list, and then the error comes when the program tries to add s[2] to something.
1

I got the same error with the following python code:

retList = []
for anItem in aList:
   if anItem % 2 == 0:
       retList = retList + anItem
return retList

when I changed the "+" which I used for concatenation to an append statement:

retList = []
for anItem in aList:
    if anItem % 2 == 0:
        retList.append(anItem) 
return retList

it worked fine.

1 Comment

Just a small caveat.. This code will only work if anItem is not a list type since + operand is more equivalent to list.extend() than list.append(). That is to say, when anItem is [3] but not 3, the result will be different.
0

I had this error in a function as a conflict of a filled global vs. unused local variable: by mistake, I added something to a numerical local variable that was not known in the function, but known as a global variable of type string.

Cutting it down to a small and reproducible example:

def test():
    for j in range(2):
        print(j)
        print(i + 1) # error here, string type + 1!!
    return

i = ['a']
test()

which will output:

<ipython-input-281-10d295524223> in test()
      2     for j in range(2):
      3         print(j)
----> 4         print(i + 1)
      5     return
      6 

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

The global variable i is the string 'a'. You cannot add + 1 to a string.

Right function would be, of course:

def test():
    for i in range(2):
        print(i + 1)
    return

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.