Let\'s say I have this list of asterisks, and I say it to print this way:
list = [\'* *\', \'*\', \'* * *\', \'* * * * *\', \'* * * * * *\', \'* * * *\']
for
How about this, for a version that mostly uses basic Python operations:
data = ['* *', '*', '* * *', '* * * * *', '* * * * * *', '* * * *']
max_len = max(len(x) for x in data) # find the longest string
for i in range(0, max_len, 2): # iterate on even numbered indexes (to get the *'s)
for column in data: # iterate over the list of strings
if i < len(column):
print column[i], # the comma means no newline will be printed
else:
print " ", # put spaces in for missing values
print # print a newline at the end of each row
Example output:
* * * * * *
* * * * *
* * * *
* * *
* *
*
If you don't want to import itertools
, you can do it like this:
ell = ['* *', '*', '* * *', '* * * * *', '* * * * * *', '* * * *']
unpadded_ell = [s.replace(' ', '') for s in ell]
height = len(max(unpadded_ell))
for s in zip(*(s.ljust(height) for s in unpadded_ell)):
print(' '.join(s))
Note a couple of things:
ell
, since list
is a built-in word in python.>>> from itertools import izip_longest
>>> list_ = ['a', 'bc', 'def']
>>> for x in izip_longest(*list_, fillvalue=' '):
... print ' '.join(x)
...
a b d
c e
f
string[] myList = null;
myList = {'*', '* *', '* * *', '* * * *', '* * * * *', '* * * * * *'};
for(int i=myList.Length-1; i>=0, i--) {
Console.WriteLine(myList[i].ToString());
}
myList = ['* *', '*', '* * *', '* * * * *', '* * * * * *', '* * * *']
import itertools
for i in itertools.izip_longest(*myList, fillvalue=" "):
if any(j != " " for j in i):
print " ".join(i)
Output
* * * * * *
* * * * *
* * * *
* * *
* *
*