Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Comparing an item in a list to an integer gives TypeError Python

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading