-2

I have a bunch of objects in a list that have a field called "number". Let the list be called "collected".

How would I find the object in the list that has "4" in its field?

I tried

class stats:
    def __init__(self, numba):
        self.Number = int(numba)

if (collected.Number == 4):
 #do stuff

However, I get an error that says 'list' object has no attribute 'number' And even if this worked, how would I be able to see which index of the list has even found the "4"?

2 Answers2

0

Do you mean:

def findindex(lst,value):
    for i,n in enumerate(lst):
        if n.Number == value:
            return i
    return -1
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
0

If stats is a list containing instances of Data then the stats list doesn't have a Number attribute, the elements do.

stats[i].Number would be legal, for any i in range(len(stats)).

Matiiss
  • 5,970
  • 2
  • 12
  • 29