python 3.5 : search different values in a list

标签 python string list search

我有一个列表:

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/

相关文章:

Python - SQLAlchemy 使用exists() 检查数据是否已在表中?

python - 嵌套列表到字典

c++ - 如何存储类似库存的数字列表?

python - return 语句如何在函数中工作?我可以使用多个 return 语句吗?

python pandas column dtype = object 导致合并失败 : DtypeWarning: Columns have mixed types

python - Bokeh DataTable Selection 时触发事件

python - 为什么 Python 根据内容使用不同的引号来表示字符串?

python - 如何从给定的字符串中提取字符和数值?

string - Typescript 类型 'string' 不可分配给类型 'never' 。错误

c - C 中查找元音计数的字符串