简体   繁体   English

如何打开嵌套列表中的元组?

[英]How to unpack tuples in nested list?

I'm having trouble unpacking a 2-dimensional list of tuples (or rather, I'm looking for a more elegant solution). 我在解开二维元组列表时遇到了麻烦(或者,我正在寻找一种更优雅的解决方案)。

The list is as shown: 列表如下所示:

a = [ [(2, 3, 5), (3, 4, 6), (4, 5, 7), (1, 1, 1), (1, 2, 3)],
      [(4, 9, 2), (8, 8, 0), (3, 5, 1), (2, 6, 8), (2, 4, 8)],
      [(8, 7, 5), (2, 5, 1), (9, 2, 2), (4, 5, 1), (0, 1, 9)], ...]

And I want to unpack the tuples to obtain 3 nested lists of the same form, ie: 我想打开元组以获取3个相同形式的嵌套列表,即:

a_r = [ [2, 3, 4, 1, 1] , [4, 8, 3, 2, 2] , [8, 2, 9, 4, 0] , ...]
a_g = [ [3, 4, 5, 1, 2] , [9, 8, 5, 6, 4] , [7, 5, 2, 5, 1] , ...]

and so on. 等等。 Here is my code: 这是我的代码:

a_r = []
a_g = []
a_b = []

for i in xrange(len(a)):
    r0=[]
    g0=[]
    b0=[]
    for j in range(5):
        r0.append(a[i][j][0])
        g0.append(a[i][j][1])
        b0.append(a[i][j][2])
    a_r.append(r0)
    a_g.append(g0)
    a_b.append(b0)

I'm sure there are more efficient ways to do this (I've just begun learning Python). 我确信有更有效的方法可以做到这一点(我刚刚开始学习Python)。 This question is similar, but I wasn't able to follow the functional programming. 这个问题很相似,但是我无法遵循函数式编程。

Thanks! 谢谢!

I think you are after something like this: 我认为您正在追求这样的事情:

a = [ [(2, 3, 5), (3, 4, 6), (4, 5, 7), (1, 1, 1), (1, 2, 3)],
      [(4, 9, 2), (8, 8, 0), (3, 5, 1), (2, 6, 8), (2, 4, 8)],
      [(8, 7, 5), (2, 5, 1), (9, 2, 2), (4, 5, 1), (0, 1, 9)]]

for row in a:
    print(list(zip(*row)))

Which gives: 这使:

[(2, 3, 4, 1, 1), (3, 4, 5, 1, 2), (5, 6, 7, 1, 3)]
[(4, 8, 3, 2, 2), (9, 8, 5, 6, 4), (2, 0, 1, 8, 8)]
[(8, 2, 9, 4, 0), (7, 5, 2, 5, 1), (5, 1, 2, 1, 9)]

The resulting tuples are same as in your example, but different order. 结果元组与您的示例相同,但顺序不同。 I dont understand how you ordered them. 我不明白您如何订购它们。 If you could clarify this, I might modify the example. 如果您可以澄清这一点,我可以修改示例。

Hope this helps. 希望这可以帮助。

>>> a = [ [(2, 3, 5), (3, 4, 6), (4, 5, 7), (1, 1, 1), (1, 2, 3)],
...       [(4, 9, 2), (8, 8, 0), (3, 5, 1), (2, 6, 8), (2, 4, 8)],
...       [(8, 7, 5), (2, 5, 1), (9, 2, 2), (4, 5, 1), (0, 1, 9)]]
>>> zip(*(zip(*x) for x in a))
[((2, 3, 4, 1, 1), (4, 8, 3, 2, 2), (8, 2, 9, 4, 0)), ((3, 4, 5, 1, 2), (9, 8, 5, 6, 4), (7, 5, 2, 5, 1)), ((5, 6, 7, 1, 3), (2, 0, 1, 8, 8), (5, 1, 2, 1, 9))]

>>> for row in _:
...     print row
... 
((2, 3, 4, 1, 1), (4, 8, 3, 2, 2), (8, 2, 9, 4, 0))
((3, 4, 5, 1, 2), (9, 8, 5, 6, 4), (7, 5, 2, 5, 1))
((5, 6, 7, 1, 3), (2, 0, 1, 8, 8), (5, 1, 2, 1, 9))

If it must be lists 如果必须是清单

>>> map(list, zip(*(map(list, zip(*x)) for x in a)))
[[[2, 3, 4, 1, 1], [4, 8, 3, 2, 2], [8, 2, 9, 4, 0]], [[3, 4, 5, 1, 2], [9, 8, 5, 6, 4], [7, 5, 2, 5, 1]], [[5, 6, 7, 1, 3], [2, 0, 1, 8, 8], [5, 1, 2, 1, 9]]]
>>> for row in _:
...     print row
... 
[[2, 3, 4, 1, 1], [4, 8, 3, 2, 2], [8, 2, 9, 4, 0]]
[[3, 4, 5, 1, 2], [9, 8, 5, 6, 4], [7, 5, 2, 5, 1]]
[[5, 6, 7, 1, 3], [2, 0, 1, 8, 8], [5, 1, 2, 1, 9]]

Tuples are immutable (meaning you cannot change them), so you're going to have to go through every element to extract them. 元组是不可变的(意味着您无法更改它们),因此您将必须遍历每个元素来提取它们。 That is essentially what the functional programming example you linked to is doing, in a very pythonic, list comprehension, way. 从本质上讲,这就是您链接到的函数式编程示例以非常Python化的列表理解方式所做的事情。

To help you help yourself, as shirdharama says, take a look at the example again and know that fmap_lol really means "function mapping - list of lists", fmap_list means "function mapping - list", and fmap_el means "function mapping - element". 为了帮助自己,如shirdharama所说,请再次看一下该示例,并了解fmap_lol的确表示“功能映射-列表列表”,fmap_list表示“功能映射-列表”,fmap_el表示“功能映射-元素” 。 So split_nt calls fmap_lol, which splits your input in a list of lists (the tuples), calls fmap_list, which takes each list and calls fmap_el, which returns the element back up the chain. 因此split_nt调用fmap_lol(将您的输入拆分为列表(元组)),调用fmap_list(获取每个列表)并调用fmap_el,fmap_el将元素返回到链上。

The example is just a way to walk through the tuples to extract every element and put them where you wish. 该示例只是一种遍历元组以提取每个元素并将其放置在所需位置的方法。 It's functional because they are functions calls, not a single line of later forgotten gobbligook. 之所以起作用,是因为它们是函数调用,而不是后来被遗忘的gobbligook的任何一行。

If you're unfamiliar with list comprehensions, lamda (or anonymous functions), or anything else in the example I suggest spending some time with the python documentation and looking each up. 如果您不熟悉列表推导,lamda(或匿名函数)或示例中的其他任何内容,建议您花一些时间阅读python文档并进行查找。

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

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