我有这个列表:
dc = ["hello", "world"]
还有这个:
lines = ["This is", "an example of hello", "line in the world of strings", "Testing"]
我希望在 dc 上找到属于 lines 的任何元素的那些元素...
我可以将行循环为:
for line in lines:
# what to do here?
但我不知道如何准确地找到 dc 中的“hello”元素是在 lines 的“an example of hello”元素中找到的,还是 dc 中的“world”是在“line in the world”中找到的行中的字符串”...
也许我不应该遍历行?
请您参考如下方法:
>>> dc = ["hello", "world", "foo"]
>>> lines = ["This is", "an example of hello", "line in the world of strings", "Testing"]
>>> [word for word in dc if any(word in line for line in lines)]
['hello', 'world']
