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]
2

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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

4 Comments

This worked, thank you so much! One last thing: any idea on how to print the 'XX' (flipped cards) before the visible cards?
reversed(list(zip_longest(...)))
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?
zip_longest(*map(reversed,stacks),fillvalue="") to reverse each stack instead of just the order they are printed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.