将元组组合成列表

时间:2015-01-24 17:39:14

标签: python tuples

在Python中,使用公共索引对元组进行分组的最佳方法是什么?

(2, 3, 'z')
(1, 1, 'abc')
(2, 1, 'stu')
(1, 2, 'def')
(2, 2, 'vxy')

结果将是:

[((1, 1, 'abc'),(1, 2, 'def')]
[((2, 1, 'stu'),(2, 2, 'vxy'), (2, 2, 'vxy')]

目标是将第3个元素连接成单个字符串对象。

这是concat部分,但我不确定分组。

def sort_tuples(list_input):
    new = sorted(list_input)
    str = ''
    for i in range(0, len(new)):
        str = str + new[i][2]
    return str

1 个答案:

答案 0 :(得分:1)

使用字典分组;选择你的分组元素,并将你想要连接的内容附加到每个键的列表:

groups = {}
for first, second, third in list_input:
    groups.setdefault(first, []).append(third)

然后你可以连接每个列表:

for key, group in groups.items():
    print(key, ''.join(group))

因为你只想连接每个元组的第三个元素,所以我没有费心在字典中包含第二个元素,但你也可以自由地将整个元组存储在组列表中。

演示:

>>> list_input = [
...     (2, 3, 'z'),
...     (1, 1, 'abc'),
...     (2, 1, 'stu'),
...     (1, 2, 'def'),
...     (2, 2, 'vxy'),
... ]
>>> groups = {}
>>> for first, second, third in list_input:
...     groups.setdefault(first, []).append(third)
... 
>>> for key, group in groups.items():
...     print(key, ''.join(group))
... 
1 abcdef
2 zstuvxy

如果第二个密钥被用作排序密钥,那么在分组时你必须包括它;然后你可以排序并提取第三个:

groups = {}
for first, second, third in list_input:
    groups.setdefault(first, []).append((second, third))

for key, group in groups.items():
    print(key, ''.join([third for second, third in sorted(group)]))

演示:

>>> groups = {}
>>> for first, second, third in list_input:
...     groups.setdefault(first, []).append((second, third))
... 
>>> for key, group in groups.items():
...     print(key, ''.join([third for second, third in sorted(group)]))
... 
1 abcdef
2 stuvxyz

由于这涉及排序,您可以对整个输入列表进行一次排序,并在排序后使用itertools.groupby()对输入进行分组:

from itertools import groupby

for key, group in groupby(sorted(list_input), key=lambda t: t[0]):
    print(key, ''.join([third for first, second, third in group]))

再一次,演示了这种方法:

>>> from itertools import groupby
>>> for key, group in groupby(sorted(list_input), key=lambda t: t[0]):
...     print(key, ''.join([third for first, second, third in group]))
... 
1 abcdef
2 stuvxyz

字典分组方法是一种O(N)算法),只要添加排序,它就会成为O(NlogN)算法。