0

I'm setting up a game of solitaire and I'm trying to figure out some ways that I could print each list of cards in column format. Any ideas on how I could go about doing this with the following lists?

[6♦]
[2♣, 6♠, A♣, 7♣, J♣, XX]
[4♥, 2♥, 4♠, 8♣, 5♦, XX, XX]
[5♠, 3♦, A♠, 10♦, 3♠, XX, XX, XX]
[7♥, 10♣, 10♥, 2♦, J♠, XX, XX, XX, XX]
[8♦, 3♣, 7♦, 9♥, K♠, XX, XX, XX, XX, XX]
[7♠, Q♠, 9♠, A♦, 3♥, XX, XX, XX, XX, XX, XX]
Friz_96
  • 13
  • 2
  • 1
    Look at [this question](http://stackoverflow.com/questions/4937491/matrix-transpose-in-python) and check out `itertools.zip_longest`. – TigerhawkT3 Apr 16 '16 at 03:47
  • Will this help ? http://stackoverflow.com/questions/12292970/write-a-list-to-columns – Amit Baswa Apr 16 '16 at 03:48

2 Answers2

3

Taking some guesswork on what you have available in your code and what you want to do, I would say that you should print an element from each list on a row and then move to the next list.

# -*- coding: utf-8 -*-
from itertools import izip_longest

L1 = [u'6♦']
L2 = [u'2♣', u'6♠', u'A♣', u'7♣', u'J♣', u'XX']
L3 = [u'4♥', u'2♥', u'4♠', u'8♣', u'5♦', u'XX', u'XX']

for a,b,c in izip_longest(L1, L2, L3, fillvalue=' '):
    print u'{0}\t{1}\t{2}'.format(a,b,c)

With few changes, you should get what you are looking for. However for more serious terminal game UI, you should consider using python curses.

Cyb3rFly3r
  • 1,321
  • 7
  • 12
  • 1
    Note that my answer is Python 2.7 code, while Tadhg McDonald-Jensen's answer is Python3. Pick the one that suits your environment better. – Cyb3rFly3r Apr 16 '16 at 04:00
2

As mentioned by others, itertools.zip_longest is definitely what you are looking for

from itertools import zip_longest

stacks = [
            ['6♦'],
            ['2♣', '6♠', 'A♣', '7♣', 'J♣', 'XX'],
            ['4♥', '2♥', '4♠', '8♣', '5♦', 'XX', 'XX'],
            ['5♠', '3♦', 'A♠', '10♦', '3♠', 'XX', 'XX', 'XX'],
            ['7♥', '10♣', '10♥', '2♦', 'J♠', 'XX', 'XX', 'XX', 'XX'],
            ['8♦', '3♣', '7♦', '9♥', 'K♠', 'XX', 'XX', 'XX', 'XX', 'XX'],
            ['7♠', 'Q♠', '9♠', 'A♦', '3♥', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX']
         ]

for cards in zip_longest(*stacks,fillvalue=""):
    print(" ".join("%3s"%c for c in cards))

results in this output:

 6♦  2♣  4♥  5♠  7♥  8♦  7♠
     6♠  2♥  3♦ 10♣  3♣  Q♠
     A♣  4♠  A♠ 10♥  7♦  9♠
     7♣  8♣ 10♦  2♦  9♥  A♦
     J♣  5♦  3♠  J♠  K♠  3♥
     XX  XX  XX  XX  XX  XX
         XX  XX  XX  XX  XX
             XX  XX  XX  XX
                 XX  XX  XX
                     XX  XX
                         XX
Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
  • This worked, thank you so much! One last thing: any idea on how to print the 'XX' (flipped cards) before the visible cards? – Friz_96 Apr 16 '16 at 04:27
  • [`reversed`](https://docs.python.org/3.5/library/functions.html#reversed)`(list(zip_longest(...)))` – Tadhg McDonald-Jensen Apr 16 '16 at 04:29
  • Doing this leaves a large amount of whitespace before any instance of 'XX'. Is there a way to align each column to the first instance of 'XX' rather than the first unflipped card? – Friz_96 Apr 16 '16 at 04:42
  • `zip_longest(*map(reversed,stacks),fillvalue="")` to reverse each stack instead of just the order they are printed. – Tadhg McDonald-Jensen Apr 16 '16 at 04:53