Python: Find in list

后端 未结 10 915
故里飘歌
故里飘歌 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条回答
  •  鱼传尺愫
    2020-11-22 10:50

    Definition and Usage

    the count() method returns the number of elements with the specified value.

    Syntax

    list.count(value)
    

    example:

    fruits = ['apple', 'banana', 'cherry']
    
    x = fruits.count("cherry")
    

    Question's example:

    item = someSortOfSelection()
    
    if myList.count(item) >= 1 :
    
        doMySpecialFunction(item)
    

提交回复
热议问题