简体   繁体   English

Python:将列表中的元素相互比较

[英]Python: Compare elements in a list to each other

I'm currently looking for a way to compare elements of a list to one another going from left to right. 我正在寻找一种从左到右比较列表元素的方法。

Here is my list: 这是我的清单:

mylist = [[15], [14, 15], [19, 20], [13], [3], [65, 19], [19, 20, 31]]

I need to compare the first element to all others and check if any of the values match, same thing for the second and third element and so on until it reaches the end of the list. 我需要将第一个元素与所有其他元素进行比较,并检查是否有任何值匹配,第二个和第三个元素是否相同,依此类推,直到它到达列表的末尾。 If there is a match, I need to print it out. 如果匹配,我需要将其打印出来。 I need to print out the value that has match and the index that it matched at. 我需要打印出匹配的值和它匹配的索引。

For example. 例如。

index 0 matched with index 1 for value 15.
index 2 matched with index 5 for value 19
index 2 matched with index 6 for value 19
index 2 matched with index 6 for value 20
index 5 matched with index 6 for value 19.

How can I go about doing this? 我该怎么做呢?

The naive solution is to loop over every pair, which is slow. 天真的解决方案是循环每一对,这很慢。 However, you can do something along the lines of this: 但是,您可以按照以下方式执行某些操作:

  • Create a dict that will map ints (elements in your nested list) to lists containing the indices of the lists in your master. 创建一个将int(嵌套列表中的元素)映射到包含主列表中索引的列表的dict。
  • Loop over the master list, and for each sublist, add its index to the spot corresponding to each of its elements in the dict. 循环遍历主列表,并为每个子列表将其索引添加到与dict中每个元素对应的点。
  • Now, the dict has values consisting of lists in which each two elements are a "pair". 现在,dict具有由列表组成的值,其中每两个元素是“对”。

This is what I mean: 这就是我的意思:

>>> mylist = [[15], [14, 15], [19, 20], [13], [3], [65, 19], [19, 20, 31]]
>>> 
>>> pairs = dict()
>>> 
>>> 
>>> from collections import defaultdict
>>> 
>>> pairs = defaultdict(list)
>>> 
>>> for idx, sub in enumerate(mylist):
...     for a in sub:
...         pairs[a].append(idx)
... 
>>> pairs
defaultdict(<type 'list'>, {65: [5], 3: [4], 13: [3], 14: [1], 15: [0, 1], 19: [2, 5, 6], 20: [2, 6], 31: [6]})

You can disregard the value lists with only 1 element (since that means we don't have a pair). 您只能忽略1个元素的值列表(因为这意味着我们没有一对)。 Those with 2+ elements form a variety of pairs. 具有2+元素的那些形成各种对。 For instance with 19 we have [2, 5, 6] meaning: 例如19我们[2, 5, 6]意思是:

  • 2 and 5 are a pair 2和5是一对
  • 2 and 6 are a pair 2和6是一对
  • 5 and 6 are a pair 5和6是一对

as you have. 就像你一样。 This approach is linear in the total number of items within your nested lists. 此方法在嵌套列表中的项目总数中是线性的。 If you have a reasonable upper bound on the nested list lengths, then this is O(n) in the size of your master list. 如果嵌套列表长度有合理的上限,那么这是主列表大小的O(n)。

Now for the output: 现在输出:

>>> from itertools import combinations
>>> 
>>> for k,v in pairs.iteritems():
...     if len(v) > 1:
...         for a,b in combinations(v, 2):
...             print "Index {} matched with index {} for value {}".format(a, b, k)
... 
Index 0 matched with index 1 for value 15
Index 2 matched with index 5 for value 19
Index 2 matched with index 6 for value 19
Index 5 matched with index 6 for value 19
Index 2 matched with index 6 for value 20

You can use itertools.combinations , it saves you from the nested-loop: 您可以使用itertools.combinations ,它可以使您免于嵌套循环:

In [770]: l2=[(i,set(mylist[i])) for i in range(len(mylist))]
     ...: for (i, a), (j, b) in combinations(l2,2):
     ...:     for v in a.intersection(b):
     ...:         print "Index {} matched with index {} for value {}".format(i,j,v)
Index 0 matched with index 1 for value 15
Index 2 matched with index 5 for value 19
Index 2 matched with index 6 for value 19
Index 2 matched with index 6 for value 20
Index 5 matched with index 6 for value 19
mylist = [[15], [14, 15], [19, 20], [13], [3], [65, 19], [19, 20, 31]]
my_set_list = [set(item) for item in mylist]
for i1, item in enumerate(my_set_list):
    for i2 in xrange(i1 + 1, len(my_set_list)):
        for val in (item & my_set_list[i2]):
            print "Index {} matched with index {} for value {}".format(i1,i2,val)

Output 产量

Index 0 matched with index 1 for value 15.
Index 2 matched with index 5 for value 19.
Index 2 matched with index 6 for value 19.
Index 2 matched with index 6 for value 20.
Index 5 matched with index 6 for value 19.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM