Can't figure out how to print * and spaces vertically and not hoirzontally

I tried looking at other threads but still couldn't figure it out.

I am using this code:

numbers = [5,1,5,2,4]

for i in numbers:
  for x in range(0,i):
    print "*",
  print""

      

It prints:

* * * * * 
* 
* * * * * 
* *
* * * *

      

and etc.

I would like it to print:

    *   *
    *   *   *
    *   *   *
    *   * * *
    * * * * *

      

I understand that I have to use operators if

for spaces or *

Any help is appreciated

+3


source to share


3 answers


numbers = [5,1,5,2,4]

for h in range(max(numbers), 0, -1):
   for x in numbers:
      if x >= h:
        print '*',
      else:
        print ' ',
   print ""

      

or a shorter version.



for h in range(max(numbers), 0, -1):
    print ' '.join('*' if x >= h else ' ' for x in numbers) 

      

+2


source


In fact, you can use it *

for three different things!



>>> from itertools import izip_longest
>>> for x in reversed(list(izip_longest(*['*'*n for n in numbers], fillvalue=' '))):
...     print ' '.join(x)
... 
*   *    
*   *   *
*   *   *
*   * * *
* * * * *

      

+2


source


numbers = [5,1,5,2,4]

for m in xrange(max(numbers)-1,0,-1):
  line = map (lambda x : '*' if x >= m else ' ' , numbers)
  print " ".join(line)

      

+2


source







All Articles