检查列表是否在python中包含另一个列表

检查列表是否在python中包含另一个列表

问题描述:

我有两个列表,一个包含相册,文件对的列表,另一个仅包含有关一张照片的信息-相册(在位置0)和文件(在位置1)

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 (at position 1)

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

如何检查照片列表是否在照片列表中?类似于photo in photos表示字符串.

How to check if photo list is in photos list? Similarly like photo in photos for strings.

专辑 文件的位置无关紧要,因为不会有等于专辑 file >.

Position of album, file doesn't matter since there won't be any file equal to album.

类似于photo in photos字符串.不仅如此,完全一样. photo in photos也适用于列表中的列表:

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

针对列表的成员资格测试仅对列表进行迭代,并对每个元素使用==相等性测试以查看是否存在匹配项.您的photo列表所测试的内容等于第二个元素:

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.