Python压缩列表列表后消除了元组

时间:2015-07-03 19:03:36

标签: python list csv

如果从多个csv文件中读取此类内容:

[[['A', 'B', 'C'], 
  ['1', '2', '3'], 
  ['2', '4', '6'], 
  ['3', '6', '9'], 
  ['4', '8', '12'], 
  ['5', '10', '15'], 
  ['6', '12', '18'], 
  ['7', '14', '21'], 
  ['8', '16', '24'], 
  ['9', '18', '27']], 
 [['D', 'E', 'F'], 
  ['4', '5', '6'], 
  ['8', '10', '12'], 
  ['12', '15', '18'], 
  ['16', '20', '24'], 
  ['20', '25', '30'], 
  ['24', '30', '36'], 
  ['28', '35', '42'], 
  ['32', '40', '48'], 
  ['36', '45', '54']], 
 [['G', 'H ', 'I'], 
  ['7', '8', '9'], 
  ['14', '16', '18'], 
  ['21', '24', '27'], 
  ['28', '32', '36'], 
  ['35', '40', '45'], 
  ['42', '48', '54'], 
  ['49', '56', '63'], 
  ['56', '64', '72'], 
  ['63', '72', '81']]]

现在我想将这些列“粘贴”在一起。所以我有想法用zip做到这一点:

map(zip, *l)

这让我觉得非常接近:

[[('A', 'D', 'G'), ('B', 'E', 'H '), ('C', 'F', 'I')], 
 [('1', '4', '7'), ('2', '5', '8'), ('3', '6', '9')], 
 [('2', '8', '14'), ('4', '10', '16'), ('6', '12', '18')], 
 [('3', '12', '21'), ('6', '15', '24'), ('9', '18', '27')], 
 [('4', '16', '28'), ('8', '20', '32'), ('12', '24', '36')], 
 [('5', '20', '35'), ('10', '25', '40'), ('15', '30', '45')], 
 [('6', '24', '42'), ('12', '30', '48'), ('18', '36', '54')], 
 [('7', '28', '49'), ('14', '35', '56'), ('21', '42', '63')], 
 [('8', '32', '56'), ('16', '40', '64'), ('24', '48', '72')], 
 [('9', '36', '63'), ('18', '45', '72'), ('27', '54', '81')]]

那么有一个简短而干净的方法可以将一个内部列表中的所有数字/字母组合在一起并消除元组吗?

修改 仅供参考:现在了解更多关于函数式编程的知识,这里是我在haskell中寻找的东西(非常肯定numpy也支持这些函数)

import Data.List
zip' = map concat . transpose

3 个答案:

答案 0 :(得分:3)

import itertools

L = myHugeListFromMultipleCsvFiles
output = [list(itertools.chain.from_iterable(sub)) for sub in zip(*L)]

或者,如果你想要更有效的方式:

output = list(map(list, itertools.chain.from_iterable(zip(*L))))

答案 1 :(得分:1)

尝试在map(zip , *l)之后使用列表推导,从内部子列表中获取元素,然后将它们合并到一个列表中(展平列表列表)。

示例 -

>>> l1 = [[z for y in x for z in y] for x in map(zip, *l)]
>>> l1
[['A', 'D', 'G', 'B', 'E', 'H ', 'C', 'F', 'I'], ['1', '4', '7', '2', '5', '8', '3', '6', '9'], ['2', '8', '14', '4', '10', '16', '6', '12', '18'], ['3', '12', '21', '6', '15', '24', '9', '18', '27'], ['4', '16', '28', '8', '20', '32', '12', '24', '36'], ['5', '20', '35', '10', '25', '40', '15', '30', '45'], ['6', '24', '42', '12', '30', '48', '18', '36', '54'], ['7', '28', '49', '14', '35', '56',
 '21', '42', '63'], ['8', '32', '56', '16', '40', '64', '24', '48', '72'], ['9', '36', '63', '18', '45', '72', '27', '54', '81']]

答案 2 :(得分:1)

如果您愿意使用numpy,以下内容将起作用

>>> import numpy as np
>>> data = np.asarray(l)
>>> mat, row, col = data.shape
>>> data = data.swapaxes(0, 1)
>>> data
array([[['A', 'B', 'C'],
        ['D', 'E', 'F'],
        ['G', 'H ', 'I']],

       [['1', '2', '3'],
        ['4', '5', '6'],
        ['7', '8', '9']],

       [['2', '4', '6'],
        ['8', '10', '12'],
        ['14', '16', '18']],
...
>>> data.resize((row, mat*col))
>>> data
array([['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H ', 'I'],
       ['1', '2', '3', '4', '5', '6', '7', '8', '9'],
       ['2', '4', '6', '8', '10', '12', '14', '16', '18'],
...

不确定这是否是您想要的,但我发现它比列表推导中的map更清晰,更容易理解。

编辑:仅供参考,压缩的实现仅仅是以下内容,假设data已经是一个numpy数组 - 请注意,您也可以使用np.genfromtxt()读取csv ,你不需要复制列表。

>>> mat, row, col = data.shape
>>> data = data.swapaxes(0, 1).reshape(row, mat*col)

更难以理解的替代方案是:

>>> data = np.asarray([d.flatten() for d in data.swapaxes(0, 1)])

许多改进的解决方案

由于某种原因没有想到它,但你可以在一个明确的函数调用中做到这一点:

>>> np.hstack(data)