gpt4 book ai didi

python - 重新匹配 ('any item from a list' ) 与 python

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

我正在尝试概括一个 python 脚本,该脚本旨在分析三种特定植物,以便能够分析任何种类的物种。我有一段代码:

dump=open('dump')
for i in dump:
if re.match('CON|VEN|PLE',i):
print i

我想说:

dump=open('dump')
list = ['CON','PLE','VEN']
for i in dump:
if re.match('any item from my list',i): # How can I ask for item from a list that match?
print i

因为我必须使用来自不同列表的大量不同 ID 多次重现此代码。

最佳答案

i 是您文件中的一行,因此为了清楚起见,我将其称为 line

 if set(line.split()).intersection(list_of_words)

如果你真的想用正则表达式来做

 if re.find("|".join(sorted(list_of_words,key=len,reverse=True)),line)

应该工作...

关于python - 重新匹配 ('any item from a list' ) 与 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23787809/

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