我有一个列表:
myList = [abc123, def456, ghi789, xyz999]
我需要根据这个允许值的“子列表”在 myList 中搜索特定值:
allowed = [abc123, xyz999]
编辑:我需要检查 myList 是否包含允许的值
我已经知道如何在列表中搜索特定的子字符串,但我真的不明白如何对“子列表”执行相同的操作。
预先感谢您的帮助。
最佳答案
我不确定您想要达到什么目的,但我认为您的意思是查看允许的值是否在您的列表中。所以试试这个:
myList = ['abc123', 'def456', 'ghi789', 'xyz999']
allowed = ['abc123', 'xyz999']
for i in myList:
if i in allowed:
print("{0} in the list".format(i))
看了大家的评论,问题有点模糊, 我使用字典创建了一个解决方案来存储允许的值并检查它们是否都在 myList 中
myList = ['abc123', 'def456', 'ghi789', 'xyz999']
allowed = {'abc123' : False, 'xyz999' : False}
for i in myList:
if i in allowed:
allowed[i]=True
print((all(value == True for value in allowed.values())))
关于 python 3.5 : search different values in a list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43521360/