gpt4 book ai didi

python - 如何在列表中查找属于 Python 另一个列表中另一个元素的元素

转载 作者:太空宇宙 更新时间:2023-11-04 07:45:10 25 4
gpt4 key购买 nike

我有这个列表:

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']

关于python - 如何在列表中查找属于 Python 另一个列表中另一个元素的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9729778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com