Python 将一个列表中的单词与其他列表中的单词进行比较
我如何检查一个列表中的一个单词是否等于另一个列表中的一个单词? 例如,我有三个列表:Python 将一个列表中的单词与其他列表中的单词进行比较,python,Python,我如何检查一个列表中的一个单词是否等于另一个列表中的一个单词? 例如,我有三个列表: ["fish", "boat", "oar"], ["rod", "gunwale", "fish", "net"], ["net", "hook", "weight"] 如何检查第一个列表中的单词是否出现在任何其他列表中?例如,我如何迭代其他两个列表中的每个单词,以查看其中是否有fish这个词,boat和oar也是如此 您只需使用in运算符即可: l1 = ["fish", "boat", "oar"]
["fish", "boat", "oar"], ["rod", "gunwale", "fish", "net"], ["net", "hook", "weight"]
如何检查第一个列表中的单词是否出现在任何其他列表中?例如,我如何迭代其他两个列表中的每个单词,以查看其中是否有fish这个词,boat和oar也是如此 您只需使用in运算符即可:
l1 = ["fish", "boat", "oar"]
l2 = ["rod", "gunwale", "fish", "net"]
l3 = ["net", "hook", "weight"]
for w in l1:
if w in l2:
print 'found %s in l2!' % w
if w in l3:
print 'found %s in l3!' % w
import itertools
for lst in lists:
# this chains all lists different than the current one into one flat list
other_lists = list(itertools.chain(*filter(lambda l: l is not lst, lists)))
for elm in lst:
if elm in other_lists:
print '%s found in another list' % elm
如果要检查它是否在其他两个列表中的任何一个列表中,您可以将它们组合在一起,并在其中执行相同的检查:
if w in l2 + l3:
print 'found %s in another list!'
可以使用“设置交点”功能,例如:
s1 = set(["fish", "boat", "oar"])
s2 = set(["rod", "gunwale", "fish", "net"])
s3 = set(["net", "hook", "weight"])
commonS12 = s1 & s2#gives you common elements
您可以在一个列表中收集所有三个列表:
lists = [["fish", "boat", "oar"], ["rod", "gunwale", "fish", "net"], ["net", "hook", "weight"]]
然后循环浏览列表列表,对于当前列表中的每个元素,使用in运算符检查其是否在任何其他列表中:
l1 = ["fish", "boat", "oar"]
l2 = ["rod", "gunwale", "fish", "net"]
l3 = ["net", "hook", "weight"]
for w in l1:
if w in l2:
print 'found %s in l2!' % w
if w in l3:
print 'found %s in l3!' % w
import itertools
for lst in lists:
# this chains all lists different than the current one into one flat list
other_lists = list(itertools.chain(*filter(lambda l: l is not lst, lists)))
for elm in lst:
if elm in other_lists:
print '%s found in another list' % elm
这是一个通用答案,适用于任何数量的列表,而不仅仅是三个。根据您最近的评论,您似乎希望计算包含第一个列表元素的列表的数量。下面是一个小函数,它可以实现以下功能:
l1 = ["fish", "boat", "oar"]
l2 = ["rod", "gunwale", "fish", "net"]
l3 = ["net", "hook", "weight"]
print("Intersection between l1 and l2",list(set(l1).intersection(l2)))
print("Intersection between l2 and l3",list(set(l2).intersection(l3)))
print("Intersection between l1 and l3",list(set(l1).intersection(l3)))
print("Intersection between l1 and l2 and l3",list(set(l1).intersection(l2).intersection(l3)))
清单中的def计数,lol:
计算在列表“lol”包含的列表中传递的列表数
元素“e”。
计数=0
对于lol中的当前_列表:
如果e在当前_列表中:
计数+=1
返回计数
现在,与casraf的回答类似,遍历l1,调用函数count_in_list,其中当前元素l1作为第一个参数,包含您感兴趣的所有其他列表的列表作为第二个参数:
对于l1中的w:
打印“{}”包含在{}其他列表格式中
W
清单SW中的计数[l2,l3]
这将为您提供以下输出:
'fish' is contained in 1 other lists
'boat' is contained in 0 other lists
'oar' is contained in 0 other lists
您也可以使用列表理解来完成此操作
l1 = ["fish", "boat", "oar"]
l2 = ["rod", "gunwale", "fish", "net"]
l3 = ["net", "hook", "weight"]
print(["{0} is in {1}".format(x, [m for m, li in globals().items() if li is L]) for x in l1 for L in [l2,l3] if x in L][0])
# use globals().iteritems() in python 2
输出:
"fish is in [l1]"
如果数据的结构与OP建议的不同
question_data = ques_data = [["fish", "boat", "oar"], ["rod", "gunwale", "fish", "net"], ["net", "hook", "weight"]]
我们可能需要采取不同的方法。此外,在for循环中执行此操作非常重要,这样无论列表有多长,它都可以进行搜索:
l1_values = ques_data[0]
remaining_list = ques_data[1::]
print(l1_values) # gives
['fish', 'boat', 'oar']
print(remaining_list) # gives
[['rod', 'gunwale', 'fish', 'net'], ['net', 'hook', 'weight']]
count = 1
for x in l1_values:
for lists in remaining_list:
count =+ 2
if x in lists:
print(x + ' is in list #' + str(count))
# gives the answer as:
fish is in list #2
print(x + ' is in list ' + str(lists))
# gives the answer as:
fish is in list ['rod', 'gunwale', 'fish', 'net']
是否要将一个列表与其他列表进行比较?或者一个接一个?一个接一个。请提供可选输入和所需输出。不知道你想做什么。这是什么背景,你真正想做什么?因为集合在这里可能很有用。或者不是。取决于上下文。我最终要做的是根据一个列表中的一个单词是否在无列表、一个列表或两个列表中返回一个计数。这个答案的一个小补充是:OP希望检查第一个列表的元素是否出现在任何其他列表中,这意味着他/她可能根本不在乎它是l2还是l3。在这种情况下,即使l2+l3:中的w也足够了。谢谢,我正在更新答案:为什么代码的输出在l2!中的find fish处停止。这不是报告重复的值net@everestial007:因为OP只对l1的元素感兴趣,l1不包含net,至少问题是这样的。哦,好的。我错过了那部分。