简体   繁体   English

比较python中不同列表中的元素

[英]Compare elements in different lists in python

So I have a question about how to compare elements in different lists in python. 所以我有一个关于如何比较python中不同列表中的元素的问题。

I have lists like this: 我有这样的清单:

[element1, element2, element3, element4, ... ]
[element1, element2, element3, element4, ... ]
[element1, element2, element3, element4, ... ]
[element1, element2, element3, element4, ... ]

So what I want is to compare element2 in each list, if element2 are the same, I want to keep only one list which contains the largest element 3 (assume element 3 is integer). 所以我想比较每个列表中的element2,如果element2相同,我只想保留一个包含最大元素3的列表(假设元素3是整数)。 So in the end the element2 in all lists are unique. 因此最后,所有列表中的element2都是唯一的。

I'm new in Python, appreciate your patients! 我是Python新手,感谢您的患者!

Check if all second element are equal: 检查所有第二个元素是否相等:

l1 = [1,2,7,4]
l2 = [1,2,4,5]
l3 = [3,2,6,5]

print len(set(zip(l1,l2,l3)[1])) == 1  # zip all lists and check all elements are equal using a set
True

zip(l1,l2,l3) looks like [(1, 1, 3), (2, 2, 2), (6, 4, 6), (6, 5, 5)] where zip(l1,l2,l3)[1] = (2, 2, 2) , as set values are unique if the len is > 1 then we don't have all common second elements. zip(l1,l2,l3)看起来像[(1, 1, 3), (2, 2, 2), (6, 4, 6), (6, 5, 5)] zip(l1,l2,l3) [(1, 1, 3), (2, 2, 2), (6, 4, 6), (6, 5, 5)] zip(l1,l2,l3)[1] = (2, 2, 2) [(1, 1, 3), (2, 2, 2), (6, 4, 6), (6, 5, 5)] ,其中zip(l1,l2,l3)[1] = (2, 2, 2) ,因为如果len> 1则设置值是唯一的,那么我们没有所有公共的第二个元素。

Get list with largest third element: 获取具有最大的第三个元素的列表:

print max(l1,l2,l3,key=lambda x : x[2]) # get list with greatest third element value
[1, 2, 7, 4]

If you have a tie for the biggest third element you can go to the fourth: 如果您对第三个最大元素持平,则可以转到第四个:

l1 = [1,2,6,6]
l2 = [1,2,4,5]
l3 = [3,2,6,5]

print max(l1,l2,l3,key=lambda x : ((x[2]),x[3]))
[1, 2, 6, 6]

It may not be the most elegent but this works: 它可能不是最高级的,但是可以这样做:

from collections import defaultdict

a = [1,2,3,4,5]
b = [2,2,5,5,3]
c = [5,2,8,1,1]
d = [1,1,1,1,1]
e = [5,1,2,3,4]
f = [1,1,5,5,5]

all_lists = [a,b,c,d,e,f]

dictionary = defaultdict(list)
for l in all_lists:
    dictionary[l[1]].append(l)

sorted_dict = {key:sorted(l, key=lambda x:x[2], reverse=True)[0] for key,l in dictionary.items()}

Hope this helps! 希望这可以帮助!

edit: Useless line 编辑:无用的线

First it groups the list by key value. 首先,它按键值对列表进行分组。 Then finds largest element based on another key 然后根据另一个键找到最大的元素

**Fixed To allow for unsorted lists/non consecutive groups**

def uniqueGroupBy(iterable,key = lambda x:x):
"grabs the groups of the iterable as a dictionary with list elements"
groups = {}
for each in iterable:
    dictKey = key(each)
    if groups.get(dictKey): groups[dictKey].append(each)
    else: groups[dictKey] = [each]
return groups


def cmpElementsByKey(a):
l = []
for k, g in uniqueGroupBy(a,key=lambda x:x[1]).iteritems(): #groups by the unique key which is your second item in the list
    l.append(max(g,key=lambda x:x[2]))    #chooses the list with the largest element 3
return l

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

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