【发布时间】:2020-10-31 17:05:39
【问题描述】:
在我的代码中,我试图将“匹配”中的项目与“数据”列表中的字符串相匹配。
我希望代码查看“匹配”列表中的第一个单词,如果它与数据“列表”中的字符串匹配,那么它将被添加到另一个列表中。 我要做的第二个检查是“匹配”列表中的前两个单词是否与数据中的字符串匹配。
目前我的输出只给了我一个 water12 的实例——而这两个实例都应该被拾取。
请有人告诉我哪里可能出错了?
match =['f helo','happy hellp','floral', 'alpha','12133','water12 puppies']
data=['f we are', 'hello there', 'alpha beta','happy today is the case','112133 is it', 'floral is my fave', 'water12 if healthy','water12 puppies are here and exist']
lst=[]
for i in match:
for j in data:
if i.split()[0] in j:
lst.append(j)
data.remove(j)
break
if len(i) > 1:
k= ' '.join(i.split()[:2])
if k in j:
lst.append(j)
data.remove(j)
break
else:
lst.append(i + ' - not found')
print(lst)
期望的输出:
output= [ 'f we are', 'alpha beta','happy today is the case','112133 is it', 'floral is my fave', 'water12 if healthy','water12 puppies are here and exist']
【问题讨论】:
标签: python python-3.x for-loop if-statement nested-loops