我怎样才能检查一个列表中的单词是否等于另一个列表中的单词? 例如,我有三个列表:
["fish", "boat", "oar"], ["rod", "gunwale", "fish", "net"], ["net", "hook", "weight"]
如何检查第一个列表中的单词是否出现在任何其他列表中?例如,我如何迭代其他两个列表中的每个单词以查看单词" fish"在他们身上,和#34;船一样#34;并且"桨。"
答案 0 :(得分:7)
您可以使用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
如果你想检查它是否在其他两个列表的任何一个中,你可以组合它们并在里面进行相同的检查:
if w in l2 + l3:
print 'found %s in another list!'
答案 1 :(得分:2)
您可以使用set intersection功能,例如:
s1 = set(["fish", "boat", "oar"])
s2 = set(["rod", "gunwale", "fish", "net"])
s3 = set(["net", "hook", "weight"])
commonS12 = s1 & s2#gives you common elements
答案 2 :(得分:2)
根据您的最新评论,您似乎希望计算包含第一个列表元素的列表数量。这是一个小功能,它正是这样做的:
def count_in_lists(e, lol):
"""
Count how many of the lists passed in the list of lists 'lol' contain
the element 'e'.
"""
count = 0
for current_list in lol:
if e in current_list:
count += 1
return count
现在,类似于casraf的回答,遍历l1
,并使用当前元素count_in_lists()
作为第一个参数调用函数l1
,并列出包含所有其他列表的列表'作为第二个参数感兴趣:
for w in l1:
print("'{}' is contained in {} other lists".format(
w,
count_in_lists(w, [l2, l3])))
这为您提供了此输出:
'fish' is contained in 1 other lists
'boat' is contained in 0 other lists
'oar' is contained in 0 other lists
答案 3 :(得分:1)
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)))
答案 4 :(得分:0)
您可以在一个列表中收集所有三个列表:
lists = [["fish", "boat", "oar"], ["rod", "gunwale", "fish", "net"], ["net", "hook", "weight"]]
然后遍历列表列表,并对当前列表中的每个元素使用in
运算符检查它是否在任何其他列表中:
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
这是一个通用答案,适用于任意数量的列表,而不仅仅是三个。
答案 5 :(得分:0)
您也可以使用列表理解来完成此操作。
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]"
答案 6 :(得分:0)
如果数据与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']