Home > database >  Comparing an item in a list to an integer gives TypeError Python
Comparing an item in a list to an integer gives TypeError Python

Time:11-12

I have an array in my python program called ageArray. It contains the same attribute from each object in a group. Here's the intitialisation code:

ageArray = [[amoeba.age] for amoeba in amoebas]

Because the I want the attribute to change, I intitialise it at the start of a while statement. After this I have the following two lines of code:

for amoeba in amoebas:
    amoeba.age = amoeba.age   1

This is intended to add 1 to each age attribute, which will then be copied over to the ageArray the next time the while loop is iterated.

The use for this array is to add an extra requirement when two of the amoeba(objects) collide, as well as checking their x and y coords, I use this:

if ageArray[i] >= 10 and ageArray[h] <= 10:

This code is intended to make sure that the ages of the amoebae are more than 10 (the reason for this is complex and so I won't explain). For some reason this piece of code is throwing up this error:

TypeError: '>' not supported between instances of 'list' and 'int'. 

Furthermore, is my code for adding 1 to each amoeba.age attribute correct? Tried using lambda with agearray but couldn't get it to work.

CodePudding user response:

You create a list with List Comprehensions, where each element is a list with one element ([amoeba.age] is a list with a single element):

ageArray = [[amoeba.age] for amoeba in amoebas]

Just leave out the inner square brackets to create a list:

ageArray = [amoeba.age for amoeba in amoebas]

CodePudding user response:

Initialise your list like this:

ageArray = [amoeba.age for amoeba in amoebas]

The way you are initialising is, creating a list of lists, that's why it is giving that type error.

  • Related