Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying a list of items vertically in a table instead of horizontally

I have a list of items sorted alphabetically:

mylist = [a,b,c,d,e,f,g,h,i,j]

I'm able to output the list in an html table horizonally like so:

| a , b , c , d |
| e , f , g , h |
| i , j ,   ,   |

What's the algorithm to create the table vertically like this:

| a , d , g , j |
| b , e , h ,   |
| c , f , i ,   |

I'm using python, but your answer can be in any language or even pseudo-code.

like image 930
MichaelMM Avatar asked Jan 23 '23 09:01

MichaelMM


1 Answers

>>> l = [1,2,3,4,5,6,7,8,9,10]
>>> [l[i::3] for i in xrange(3)]
[[1, 4, 7, 10], [2, 5, 8], [3, 6, 9]]

Replace 3 by the number of lines you want as a result:

>>> [l[i::5] for i in xrange(5)]
[[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]]
like image 81
balpha Avatar answered Jan 25 '23 22:01

balpha