简体   繁体   English

在列表中查找具有相同第一项的元组并返回另一个列表

[英]Find tuple in list with same first item and return another list

I have a list like this in Python:我在 Python 中有一个这样的列表:

[('a', 'b'), ('a', 'c'),('d','f')]

and I want join items that have same first item and result like this:我想加入具有相同第一项和结果的项,如下所示:

[('a', 'b', 'c'),('d','f')]

Here is one way to do it.这是一种方法。 For efficiency, we build a dict with the first value as key.为了提高效率,我们构建了一个以第一个值作为键的dict We keep the values in the order in which they appear (and the tuples in their original order as well, if you use Python >= 3.7 - otherwise you will have to use a collections.OrderedDict )我们按照它们出现的顺序保留值(如果您使用 Python >= 3.7,则元组也按其原始顺序保存 - 否则您将不得不使用collections.OrderedDict

def join_by_first(sequences):
    out = {}
    for seq in sequences:
        try:
            out[seq[0]].extend(seq[1:])
        except KeyError:
            out[seq[0]] = list(seq)
    return [tuple(values) for values in out.values()]

join_by_first([('a', 'b'), ('a', 'c'),('d','f')])
# [('a', 'b', 'c'), ('d', 'f')]

You can not edit tuples - the are immuteable.您不能编辑tuples - 它们是不可变的。 You can use lists and convert all back to tuples afterward:您可以使用lists并在之后将所有转换回tuples

data = [('a', 'b'), ('a', 'c'),('d','f')]

new_data = []


for d in data                                             # loop over your data
    if new_data and new_data[-1][0] == d[0]:              # if something in new_data and 1st
        new_data[-1].extend(d[1:])                        # ones are identical: extend
    else:
        new_data.append( [a for a in d] )                 # not same/nothing in: add items

print(new_data)                   # all are lists

new_data = [tuple(x) for x in new_data]
print(new_data)                   # all are tuples again      

Output:输出:

[['a', 'b', 'c'], ['d', 'f']]     # all are lists
[('a', 'b', 'c'), ('d', 'f')]     # all are tuples again   

See Immutable vs Mutable types请参阅不可变与可变类型

I feel like the simplest solution is to build a dictionary in which:我觉得最简单的解决方案是构建一个字典,其中:

  • keys are the first items in the tuples键是元组中的第一项
  • values are lists comporting all second items from the tuples值是包含元组中所有第二项的列表

Once we have that we can then build the output list:一旦我们有了它,我们就可以构建输出列表:

from collections import defaultdict

def merge(pairs):
    mapping = defaultdict(list)
    for k, v in pairs:
        mapping[k].append(v)
    return [(k, *v) for k, v in mapping.items()]

pairs = [('a', 'b'), ('a', 'c'),('d','f')]
print(merge(pairs))

This outputs:这输出:

[('a', 'b', 'c'), ('d', 'f')]

This solution is in O(n) as we only iterate two times over each item from pairs .这个解决方案的复杂度为O(n),因为我们只pairs来自pairs每个项目迭代两次。

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

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