检查列表是否在python中包含另一个列表 [英] Check if list contains another list in python

查看:99
本文介绍了检查列表是否在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.

这篇关于检查列表是否在python中包含另一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆