Check if list contains another list in python

前端 未结 2 1136
刺人心
刺人心 2020-12-22 02:50

I have two lists, one containing lists of album, file pairs and the other containing only info about one photo - album (at position 0) and file (a

相关标签:
2条回答
  • 2020-12-22 03:17
    photos = [["Trip to Thailand", "IMG_001.jpg"], ["Latvia 2010", "IMG_001.jpg"]]
    photo = ["Latvia 2010", "IMG_001.jpg"]
    print (photo in photos)
    True
    

    There is no difference, you would check exactly as you would for a string.

    0 讨论(0)
  • 2020-12-22 03:28

    Similarly like photo in photos for strings. Not just similarly, exactly like that. photo in photos works for lists inside lists too:

    >>> photos = [["Trip to Thailand", "IMG_001.jpg"], ["Latvia 2010", "IMG_001.jpg"]]
    >>> photo = ["Latvia 2010", "IMG_001.jpg"]
    >>> photo in photos
    True
    

    Membership testing against a list simply iterates over the list and uses == equality testing with each element to see if there is a match. Your photo list tests as equal to the second element:

    >>> photos[1] == photo
    True
    

    because all strings in both lists are equal.

    0 讨论(0)
提交回复
热议问题