Python remove tuples after skewing a list of lists of lists

If there is something like this, read from several csv files:

[[['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']]]

      

Now I want to "insert" columns together. So I got the idea to do it with zip:

map(zip, *l)

      

This gives me this, which is pretty close:

[[('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')]]

      

So, is there a short and clean way to get all the numbers / letters in one inner list together and get rid of the tuples?

EDIT: Just for information: now knowing more about functional programming, this is what I was looking for in haskell (pretty confident numpy support also supports these features)

import Data.List
zip' = map concat . transpose

      

+3


source to share


3 answers


If you want to use numpy the following will work

>>> 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'],
...

      

Not sure if this is what you wanted, but I find it simpler and easier to understand than map

.

EDIT : FYI, the condensed implementation would be as follows: assuming is data

already a numpy array - note that you can also read the csv with np.genfromtxt()

that you don't need to copy the list.

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

      



A more obscure alternative is:

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

      

Much improved solution

Haven't thought about it for some reason, but you can do it with one unambiguous function call:

>>> np.hstack(data)

      

+1


source


import itertools

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

      

Or, if you want to go in a more functional way:



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

      

+3


source


Try using a list comprehension after map(zip , *l)

to get the items from internal sublists and then concatenate them into one list (group a list of lists into a list).

Example -

>>> 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']]

      

+1


source







All Articles