我正在尝试使用以下代码查找两个列表之间的匹配:
def matching(text, symples, half2):
for word in text:
find = set(symples).intersection(word)
indexNum = symples.index(find)
print(indexNum)
print(find)
我成功地找到了他们之间的匹配。我需要在列表中找到匹配单词的索引号,并且当我尝试在列表中找不到该单词时,会收到一条错误消息。你知道吗
我试着在两个列表find
之间打印匹配的单词,它是用括号({}
或[]
)打印的。你知道吗
括号是列表中找不到匹配项的原因吗?你知道吗
你的代码不起作用有几个原因,括号就是其中一个原因。你知道吗
行
find = set(symples).intersection(word)
返回set
并将其赋值给变量find
。稍后,当您尝试查找find的索引时,由于列表中找不到该集,因此找不到该索引。你知道吗例如:
要解决此问题,请通过交叉点集进行循环:
代码中的缩进也有很多问题。for循环一直在运行,因此
find
只设置了最后一个单词的交集。如果你想打印出每一个,确保你有正确的缩进。下面是一个示例,前面的错误也已修复:然而,有更好的方法来实现这一点。。。你知道吗
只走一次十字路口。你知道吗
没有理由在文本中循环,每次都选择交叉点。马上把这两张单子的交集取出来。你知道吗
循环浏览一个列表并检查每个单词是否在另一个列表中。你知道吗
循环浏览一个列表并检查每个单词是否在另一个列表中,同时跟踪索引。你知道吗
相关问题 更多 >
编程相关推荐