-1

I have a list that contains the rows of each column:

 table = [['10 pounds', '25 pounds', '50 pounds', '100 pounds', '250 pounds', '500 pounds', '1000 pounds'],
         [25,            58.75,       110,         200,          437.5,        750,          1350], 
         [0,             0,           0,           0,            0,            0,            0],
         [50,            50,          10,          10,           5,            5,            5]]

How would I convert it to entire rows? I have spaced out the list above to show what I am trying to get better.

I want to convert it to a list containing each row for example:

['10 pounds',25,0,50]
['25 pounds',58.75,0,50]
['50 pounds',110,0,10]
['100 po...

1 Answers1

2

That is easily achievable with numpy. Look:

>>> import numpy as np
>>> table = [['10 pounds', '25 pounds', '50 pounds', '100 pounds', '250 pounds', '500 pounds', '1000 pounds'],
...          [25,            58.75,       110,         200,          437.5,        750,          1350], 
...          [0,             0,           0,           0,            0,            0,            0],
...          [50,            50,          10,          10,           5,            5,            5]]
>>> 
>>> table
[['10 pounds', '25 pounds', '50 pounds', '100 pounds', '250 pounds', '500 pounds', '1000 pounds'], [25, 58.75, 110, 200, 437.5, 750, 1350], [0, 0, 0, 0, 0, 0, 0], [50, 50, 10, 10, 5, 5, 5]]
>>> t = np.array(table).transpose()
>>> t
array([['10 pounds', '25', '0', '50'],
       ['25 pounds', '58.75', '0', '50'],
       ['50 pounds', '110', '0', '10'],
       ['100 pounds', '200', '0', '10'],
       ['250 pounds', '437.5', '0', '5'],
       ['500 pounds', '750', '0', '5'],
       ['1000 pounds', '1350', '0', '5']], dtype='<U32')
>>> 
accdias
  • 5,160
  • 3
  • 19
  • 31
  • 1
    numpy is a bit of an overkill here if it's not already being used. As shown in the duplicate I linked, this can be easily achieved using the built-in `zip`... – Tomerikoo Dec 15 '21 at 16:57
  • For the example in the question, it is indeed. But for larger arrays, numpy is going to be the best solution always. – accdias Dec 15 '21 at 16:59
  • Still, this is [already covered in the duplicate](https://stackoverflow.com/a/6473727/6045800) so it is best to cast a close vote instead of answering with existing content – Tomerikoo Dec 15 '21 at 17:02