简体   繁体   English

使用Python将元组列表转换为嵌套列表

[英]Converting a Tuples List into a nested List using Python

I want to convert a tuples list into a nested list using Python. 我想使用Python将元组列表转换为嵌套列表。 How do I do that? 我怎么做?

I have a sorted list of tuples (sorted by the second value): 我有一个排序的元组列表(按第二个值排序):

[(1, 5),  (5, 4), (13, 3), (4, 3), (3, 2), (14, 1), (12, 1), 
 (10, 1), (9, 1), (8, 1),  (7, 1), (6, 1), (2, 1)]

Now I want it to have like this (second value ignored and nested in lists): 现在我希望它有这样的(第二个值被忽略并嵌套在列表中):

[ [1], [5], [13, 4], [3], [14, 12, 10, 9, 8, 7, 6, 2] ]

I've seen other threads in here with map used for such things, but I don't completely understand it. 我已经在这里看到了其他线程用于这些事情的map ,但我并不完全理解它。 Can anyone provide insight as to the 'correct' python way of doing this? 任何人都可以提供有关'正确'python方式的见解吗?

from operator import itemgetter
from itertools import groupby

lst = [(1, 5),  (5, 4), (13, 3), (4, 3), (3, 2), (14, 1),
       (12, 1), (10, 1), (9, 1), (8, 1),  (7, 1), (6, 1), (2, 1)]

result = [[x for x, y in group]
          for key, group in groupby(lst, key=itemgetter(1))]

groupby(lst, key=itemgetter(1)) generates groups of consecutive elements of lst within which all elements have the same 1st (counting from zero) item. groupby(lst, key=itemgetter(1))生成lst的连续元素组,其中所有元素具有相同的1st(从零开始计数)项。 The [x for x, y in group] keeps the 0th item of each element within each group. [x for x, y in group]保留每个组中每个元素的第0项。

It is a bit convoluted, but you can do it with the itertools.groupby function: 这有点令人费解,但您可以使用itertools.groupby函数执行此操作:

>>> lst = [(1, 5),  (5, 4), (13, 3), (4, 3), (3, 2), (14, 1), (12, 1), 
 (10, 1), (9, 1), (8, 1),  (7, 1), (6, 1), (2, 1)]
>>> from operator import itemgetter 
>>> import itertools
>>> [map(itemgetter(0), group) for (key,group) in itertools.groupby(lst, itemgetter(1))]
[[1], [5], [13, 4], [3], [14, 12, 10, 9, 8, 7, 6, 2]]
>>> 

Explanation: groupby returns an iterator for each group, where a group is defined as a sequence of entries that have the same value returned by function passed as a separate parameter. 说明:groupby为每个组返回一个迭代器,其中一个组被定义为一系列条目,这些条目具有由作为单独参数传递的函数返回的相同值。 itemgetter(1) generates a function that returns x[1] when called with argument x. itemgetter(1)生成一个函数,该函数在使用参数x调用时返回x [1]。 Since the groupby iterator returns two values - the key that was used and sequences of the original values which are tuples, we then need to strip out the second value in each tuple, which is what map(itemgetter(0), group) does. 由于groupby迭代器返回两个值 - 使用的键和原始值的序列是元组,因此我们需要去掉每个元组中的第二个值,这就是map(itemgetter(0),group)所做的。

Maybe not the most pythonesque answer, but this works: 也许不是最pythonesque的答案,但这有效:

d = {}

a = [(1,5), (5,4), (13,3), (4,3), (3,2), (14,1), (12,1)]

for value in a:
     if value[0] not in d:
         d[ value[0] ] = []
     d[ value[0] ].append( a[1] )

print d.values()

The simple solution: 简单的解决方案:

n_list = []
c_snd = None
for (fst, snd) in o_list:
  if snd == c_snd: n_list[-1].append(fst)
  else:
    c_snd = snd
    n_list.append([fst])

Explanation: use c_snd to store the current second part of the tuple. 说明:使用c_snd存储元组的当前第二部分。 If that changes, start a new list in n_list for this new second value, starting with fst , otherwise add fst to the last list in n_list . 如果改变了,开始在一个新的列表n_list为这个新的第二个值,从fst ,否则添加fst在最后名单n_list

Don't know how fast this will be for bigger sets, but you could do something like that: 不知道这对于更大的集合有多快,但你可以这样做:

input = [
    (1,  5), (5,  4), (13, 3), (4, 3), (3, 2), (14, 1),
    (12, 1), (10, 1), (9,  1), (8, 1), (7, 1), (6,  1),
    (2,  1)
]

output = [[] for _ in xrange(input[0][1])]
for value, key in input:
    output[-key].append(value)

print output # => [[1], [5], [13, 4], [3], [14, 12, 10, 9, 8, 7, 6, 2]]

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

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