Using 'in' to match an attribute of Python objects in an array

后端 未结 8 1529
傲寒
傲寒 2021-01-07 16:25

I don\'t remember whether I was dreaming or not but I seem to recall there being a function which allowed something like,

foo in iter_attr(array of python ob         


        
8条回答
  •  萌比男神i
    2021-01-07 16:26

    If you plan on searching anything of remotely decent size, your best bet is going to be to use a dictionary or a set. Otherwise, you basically have to iterate through every element of the iterator until you get to the one you want.

    If this isn't necessarily performance sensitive code, then the list comprehension way should work. But note that it is fairly inefficient because it goes over every element of the iterator and then goes BACK over it again until it finds what it wants.

    Remember, python has one of the most efficient hashing algorithms around. Use it to your advantage.

提交回复
热议问题