Fer*_*rra 1 python substring list subset python-3.x
我很难检查 python 列表中的所有字符串是否是另一个 python 列表中任何字符串的子集。
示例:我想检查的每个字符串(所有字符串)list1是否至少包含在其中的一个字符串中list2,如果是,请执行某些操作。
list1 = ['tomato', 'onions','egg']
list2 = ['Two tomatos', 'two onions','two eggs','salsa']
Run Code Online (Sandbox Code Playgroud)
例如,在这个例子中,它会返回True.
您可以将生成器表达式与any/all函数结合使用:
>>> list1 = ['tomato', 'onions','egg']
>>> list2 = ['Two tomatos', 'two onions','two eggs','salsa']
>>> all(any(i in j for j in list2) for i in list1)
True
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
68 次 |
| 最近记录: |