标签云

微信群

扫码加入我们

WeChat QR Code

For a list ["foo", "bar", "baz"] and an item in the list "bar", how do I get its index (1) in Python?


Are you returning:[1] The lowest index in case there are multiple instances of "bar", [2] All the indices of "bar"?

2019年05月24日13分20秒

a) Is it guaranteed that item is in the list, or else how we should handle the error case? (return None/ raise ValueError) b) Are list entries guaranteed to be unique, and should we return the first index of a match, or all indexes?

2019年05月24日13分20秒

index returns the first item whose value is "bar". If "bar" exists twice at list, you'll never find the key for the second "bar". See documentation: docs.python.org/3/tutorial/datastructures.html

2019年05月24日13分20秒

How to do this for numpy array ?

2019年05月24日13分20秒

It's stipulated in the question that the item is in the list.

2019年05月24日13分20秒

AlexCoventry: even so it's still good to show the more paranoid alternative too. Especially as this question is now canonical.

2019年05月25日13分20秒

mpoletto the scenario you describe already has another question here on SO: stackoverflow.com/questions/6294179/…. This question focuses on one index only.

2019年05月24日13分20秒

Enumeration works better than the index-based methods for me, since I'm looking to gather the indices of strings using 'startswith" , and I need to gather multiple occurrences. Or is there a way to use index with "startswith" that I couldn't figure out

2019年05月24日13分20秒

In my hands, the enumerate version is consistently slightly faster. Some implementation details may have changed since the measurement above was posted.

2019年05月24日13分20秒

This was already answered since '11: stackoverflow.com/questions/6294179/…

2019年05月24日13分20秒

There's already another question for this, added in '11: stackoverflow.com/questions/6294179/…

2019年05月24日13分20秒

And if doesn't exist in the list?

2019年05月24日13分20秒

Not-exist item will raise ValueError

2019年05月24日13分20秒

This answer would fit better here: stackoverflow.com/questions/6294179/…

2019年05月24日13分20秒

'item' is a very misleading variable name. 'index' maybe ?

1970年01月01日00分09秒

This answer should be better posted here: stackoverflow.com/questions/6294179/…

2019年05月24日13分20秒

This helps us to avoid try catch!

2019年05月24日13分20秒

The 3rd method iterates twice over the list, right?

2019年05月24日13分20秒

Re: "All of the proposed functions here": At the time of writing perhaps, but you ought to check newer answers to see if it is still true.

2019年05月24日13分20秒

What about lists of strings, lists of non-numeric objects, etc... ?

2019年05月24日13分20秒

This answer should be better posted here: stackoverflow.com/questions/6294179/…

2019年05月24日13分20秒

This answer should be better posted here: stackoverflow.com/questions/6294179/…

2019年05月24日13分20秒

This answer should be better posted here: stackoverflow.com/questions/6294179/…

2019年05月24日13分20秒

This answer should be better posted here: stackoverflow.com/questions/6294179/…

2019年05月24日13分20秒

This answer should be better posted here: stackoverflow.com/questions/6294179/…

2019年05月24日13分20秒

Downvoted for single letter variable names.

2019年05月24日13分20秒

[i for i,j in enumerate(haystack) if j==‘needle’] is more compact and readable, I think.

2019年05月24日13分20秒

This iterates the array twice, thus it could result in performance issues for large arrays.

2019年05月24日13分20秒

Agree. If lists are considerably long, I'd go for something else. Shouldn't be a big deal for small to medium sized lists though.

2019年05月24日13分20秒

This answer feels at home for Scala / functional-programming enthusiasts

2019年05月24日13分20秒