5

Let's say I have this list of asterisks, and I say it to print this way:

list = ['* *', '*', '* * *', '* * * * *', '* * * * * *', '* * * *']
for i in list:
    print i

So here, the output is:

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

But I want the output to be vertical, like this:

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

Any tips on doing this? I've tried to conceptualize how to use things like list comprehension or for-loops for this, but haven't got it quite right.

UnworthyToast
  • 825
  • 5
  • 11
  • 21
  • 2
    What you want to do is "transpose the matrix." Or print the array as a 2-dimensional character array in column-major order. – Mr. Polywhirl Nov 12 '13 at 04:08

5 Answers5

7
myList = ['* *', '*', '* * *', '* * * * *', '* * * * * *', '* * * *']
import itertools
for i in itertools.izip_longest(*myList, fillvalue=" "):
    if any(j != " " for j in i):
        print " ".join(i)

Output

* * * * * *
*   * * * *
    * * * *
      * * *
      * *  
        *  
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
4
>>> 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
wim
  • 338,267
  • 99
  • 616
  • 750
  • Hmmm....sorry, I should have been clearer! So far I have only learned to use the basic stuff in python: while loops, for loops, list comprehension, elementary stuff like that. Any solution using more primitive methods that you can think of?? Thanks :D – UnworthyToast Nov 12 '13 at 03:51
  • 2
    @UnworthyToast: `zip` (along with its friends, like `zip_longest`) is the one, obvious way to "transpose" a sequence of sequences. You _can_ do things manually, but it will be a lot harder, and more verbose, and easier to get wrong. It will serve you much better to read the documentation on the function and play with it until you understand it than to try to figure out and debug the horrible loopy equivalent. – abarnert Nov 12 '13 at 04:45
4

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:

  1. I have renamed the list to ell, since list is a built-in word in python.
  2. This works by expanding the strings so that they all have the same length by padding them with spaces, then converting the list of strings into a list of lists representing a rectangular matrix.
  3. I have used the trick described in this post to do a matrix transpose, which is what you want to print. It uses zip, which is a builtin function for "combining" iterables like lists.
  4. I also used a couple of comprehensions to keep things short.
  5. This works in python 2 and 3. If you want it to look more like python 2, take out the parentheses used for the print function.
Community
  • 1
  • 1
Apis Utilis
  • 567
  • 3
  • 12
1

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:

* * * * * *
*   * * * *
    * * * *
      * * *
      * *  
        *  
Blckknght
  • 100,903
  • 11
  • 120
  • 169
-5
string[] myList = null;
myList = {'*', '* *', '* * *', '* * * *', '* * * * *', '* * * * * *'};
for(int i=myList.Length-1; i>=0, i--) {
    Console.WriteLine(myList[i].ToString());
}