Python: Find in list

后端 未结 10 830
故里飘歌
故里飘歌 2020-11-22 10:15

I have come across this:

item = someSortOfSelection()
if item in myList:
    doMySpecialFunction(item)

but sometimes it does not work with

10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 10:55

    While the answer from Niklas B. is pretty comprehensive, when we want to find an item in a list it is sometimes useful to get its index:

    next((i for i, x in enumerate(lst) if [condition on x]), [default value])
    

提交回复
热议问题