简体   繁体   English

将第二个唯一项目与第一个项目匹配,该项目在Python列表中重复

[英]Match the second unique item to the first item which is repetive in a Python list

I am looping over a list which produces lists that contain two items, for example; 我遍历一个列表,该列表产生包含两个项目的列表,例如;

['string1', '1234567']
['string1', '1234576']
['string1', '1234765']
['string2', '7654321']
['string2', '7654123']

The first item in the list can be repetitive, the second item in the list is always unique. 列表中的第一项可以重复,列表中的第二项始终是唯一的。 I want to restructure the list so the following output is produced. 我想重组列表,以便产生以下输出。 I want to have the first items in the list to be unique with the corresponding second items. 我希望列表中的第一项与相应的第二项是唯一的。 The desired output; 所需的输出;

['string1', ['1234567', '1234576','1234765']]
['string2', ['7654321','7654123']]

Is it useful to generate a new list of the second items in the list, then create a new list to get the unique strings from the first items? 生成列表中第二项的新列表,然后创建新列表以从第一项中获取唯一字符串是否有用? Then compare the two lists and map them in some way...I really have no idea. 然后比较两个列表并以某种方式映射它们...我真的不知道。 I don't know if there is some kind of Python functionality for this? 我不知道是否有某种Python功能吗?

Since the data is sorted, you can use itertools.groupby : 由于数据已排序,因此可以使用itertools.groupby

from itertools import groupby

l = [['string1', '1234567'],
     ['string1', '1234576'],
     ['string1', '1234765'],
     ['string2', '7654321'],
     ['string2', '7654123']]

l2 = [[k, [x[1] for x in g]] for k, g in groupby(l, key=lambda x: x[0])]
# [['string1', ['1234567', '1234576', '1234765']],
#  ['string2', ['7654321', '7654123']]]

If the data weren't sorted, you could use a collections.defaultdict to collect all the second elements for each first. 如果未对数据进行排序,则可以使用collections.defaultdict为每个第一个元素收集所有第二个元素。 This is essentially the same approach that mshsayem chose in his answer where he uses a vanilla dict and setdefault : 这与mshsayem在回答中选择的方法基本相同, 后者使用的是van dictsetdefault

from collections import defaultdict

d = defaultdict(list)
for x, y in l:
    d[x].append(y)
l2 = d.items()
# [('string2', ['7654321', '7654123']), 
#  ('string1', ['1234567', '1234576', '1234765'])]

Here is a way: 这是一种方法:

>>> l = [['string1', '1234567']
,['string1', '1234576']
,['string1', '1234765']
,['string2', '7654321']
,['string2', '7654123']]
>>> result = {}
>>> for li in l:
        result.setdefault(li[0],[]).append(li[1])


>>> result
{'string2': ['7654321', '7654123'], 'string1': ['1234567', '1234576', '1234765']}

If you want list of list (as your question) you can do this: 如果要列表列表(作为您的问题),可以执行以下操作:

>>> map(list,result.items())
[['string2', ['7654321', '7654123']], ['string1', ['1234567', '1234576', '1234765']]]

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

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