0

I have a group of lists that look like this when printed out:

    =================Tableaus=================
    ---1----2----3----4----5----6----7----8---
[K♠, 5♠, 10♥, 2♥, 7♦, Q♣, 4♣]
[Q♠, 4♠, 9♥, A♥, 6♦, J♣, 3♣]
[J♠, 3♠, 8♥, K♦, 5♦, 10♣, 2♣]
[10♠, 2♠, 7♥, Q♦, 4♦, 9♣, A♣]
[9♠, A♠, 6♥, J♦, 3♦, 8♣, None]
[8♠, K♥, 5♥, 10♦, 2♦, 7♣, None]
[7♠, Q♥, 4♥, 9♦, A♦, 6♣, None]
[6♠, J♥, 3♥, 8♦, K♣, 5♣, None]

How do I turn these lists into neat columns? I also want to remove the "None" from the last 4 lists but so far my attempts to do so have given me errors.

RGPython
  • 27
  • 7
  • Removing the `None`s is easy, lining up the columns is much, *much* harder... – Jared Smith Apr 17 '17 at 21:11
  • Try looking for "pretty print" solutions, e.g. http://stackoverflow.com/questions/9712085/numpy-pretty-print-tabular-data – Riaz Apr 17 '17 at 21:14
  • Do you have a deadline for a home assignment today? See http://stackoverflow.com/questions/43456021/printing-multiple-lists-vertically/43456772#43456772 – VeGABAU Apr 17 '17 at 21:14
  • There is plenty of documentation and tutorial material on line. How is it that you're stuck? – Prune Apr 17 '17 at 21:16

1 Answers1

0

The most straightforward way to do it is (ab)using replace(). Let's say the line we want to print is called x:

x = ['7♠', 'Q♥', '4♥', '9♦', 'A♦', '6♣', None]

Then if we do:

x=str(x)
print(7*' '+x[1:-1].replace(', ','   ').replace("'",'').replace('None','    '))

It will print:

   7♠   Q♥   4♥   9♦   A♦   6♣

With the rest of the table the result would be:

    =================Tableaus=================
    ---1----2----3----4----5----6----7----8---
       7♠   Q♥   4♥   9♦   A♦   6♣       

Apply that to all the lines and you are done.

It's just doing:

  1. Change the separator: from ', ' to 3 spaces
  2. Delete some '.
  3. Delete any substring 'None' (and replace it by 4 spaces, just if a None is in the middle)

For the substring x[1:-1]. And it also adds some indentation.

Juan T
  • 1,219
  • 1
  • 10
  • 21