Python解压缩二维元组列表 [英] Python unpack 2-dimensional list of named tuples
问题描述
我有一个命名元组的二维列表(假设每个元组具有N个值),我想将它们解压缩为N个不同的二维列表,其中每个解压缩的二维列表都完全由一个单一的组成原始列表中的属性.例如,如果我有此二维列表:
I have a 2-dimensional list of named tuples (let's say that each tuple has N values), and I want to unpack them into N different 2-dimensional lists where each unpacked 2-D list is composed entirely of a single attribute from the original list. For example if I have this 2-D list:
>>> combo = namedtuple('combo', 'i, f, s')
>>> combo_mat = [[combo(i + 3*j, float(i + 3*j), str(i + 3*j)) for i in range(3)]
for j in range(3)]
>>> combo_mat
[[combo(i=0, f=0.0, s='0'), combo(i=1, f=1.0, s='1'), combo(i=2, f=2.0, s='2')],
[combo(i=3, f=3.0, s='3'), combo(i=4, f=4.0, s='4'), combo(i=5, f=5.0, s='5')],
[combo(i=6, f=6.0, s='6'), combo(i=7, f=7.0, s='7'), combo(i=8, f=8.0, s='8')]]
我希望这3个结果是:
[[0, 1, 2],
[3, 4, 5],
[6, 7, 8]]
[[0.0, 1.0, 2.0],
[3.0, 4.0, 5.0],
[6.0, 7.0, 8.0]]
[['0', '1', '2'],
['3', '4', '5'],
['6', '7', '8']]
如果我只有一维元组列表,我会使用zip(*mylist)
,例如:
If I just had a 1-dimensional list of tuples I'd use zip(*mylist)
, like:
>>> zip(*[combo(i=0, f=0.0, s='0'), combo(i=1, f=1.0, s='1'), combo(i=2, f=2.0, s='2')])
[(0, 1, 2), (0.0, 1.0, 2.0), ('0', '1', '2')]
我可以通过嵌套将其扩展到我的情况:
And I can extend this to my situation just by nesting:
>>> zip(*[zip(*combs) for combs in combo_mat])
[((0, 1, 2),
(3, 4, 5),
(6, 7, 8)),
((0.0, 1.0, 2.0),
(3.0, 4.0, 5.0),
(6.0, 7.0, 8.0)),
(('0', '1', '2'),
('3', '4', '5'),
('6', '7', '8'))]
但是,这没有给我我想要的列表,并且嵌套解压zip(*)
函数的可读性不高.任何人有更多的pythonic解决方案的想法吗?如果您可以在最终结果中的某个地方使用元组的属性名称,则可以得到加分.
But this doesn't give me the lists I wanted, and nested unpacking zip(*)
functions isn't that readable. Anyone have any ideas for a more pythonic solution? Bonus points if you can work the names of the tuples' attributes in there somewhere in the end result.
实际上,现在我想到了,如果我有一个将元组属性的名称映射到其相应矩阵的字典,那将是理想,
Actually, now that I think of it, it would be ideal if I could have a dict that mapped the name of the tuple attribute to its respective matrix, like:
{'i': [[0, 1, 2],
[3, 4, 5],
[6, 7, 8]],
'f': [[0.0, 1.0, 2.0],
[3.0, 4.0, 5.0],
[6.0, 7.0, 8.0]]
's': [['0', '1', '2'],
['3', '4', '5'],
['6', '7', '8']]}
推荐答案
功能性编程可以解救吗?本质上,这是嵌套zip的一种更清洁的版本:
Functional programming to the rescue? It's essentially a cleaner version of nesting the zips:
def fmap_element(f, el):
return f(el)
def fmap_list(f, l):
return [fmap_element(f, el) for el in l)]
def fmap_lol(f, lol):
return [fmap_list(f,l) for l in lol]
def split_nt_lol(nt_lol):
return dict((name, fmap_lol(lambda nt: getattr(nt, name), nt_lol))
for name in nt_lol[0][0]._fields)
用法:
>>> split_nt_lol(combo_mat)
{'i': [[0, 1, 2], [3, 4, 5], [6, 7, 8]],
's': [['0', '1', '2'], ['3', '4', '5'], ['6', '7', '8']],
'f': [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]}
这篇关于Python解压缩二维元组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!