0

I am trying to pretty print a list but unfortunately, I am having a little trouble with it. So the code goes like this:

from pprint import pprint
deck = []
for s in ("CDHS"):
    for v in ("A23456789TJQK"):
        deck.append(v+s)
print(deck)
pprint(deck, width=20)

I am trying to print the deck like this:

AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC
AD 2D 3D 4D 5D 6D 7D 8D 9D TD JD QD KD
AH 2H 3H 4H 5H 6H 7H 8H 9H TH JH QH KH
AS 2S 3S 4S 5S 6S 7S 8S 9S TS JS QS KS

but It is printing like this :

['AC',
 '2C',
 '3C',
 ......
 .....

please tell me how to fix it! Thanks

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
Baran
  • 29
  • 1
  • 5
  • 1
    If you search in your browser for "Python print tutorial", you'll find references that can explain this much better than we can manage here. – Prune Sep 26 '19 at 17:49
  • 2
    There is no magic pprint function that will do that for you. You have to do it yourself with a print tutorial in hand. – RickyA Sep 26 '19 at 19:09
  • 1
    If you object to the `[...]` brackets then stop using `pprint`, simply use plain old `print`. Just because you're trying to prettyprint something, doesn't follow that you must use `pprint` module. – smci Oct 02 '19 at 20:45
  • 1
    `for i in range(0, len(deck), 13): print(' '.join(deck[i:i+13]))` – inspectorG4dget Oct 02 '19 at 20:48
  • `pprint(deck, compact=True)` does give you 80-character lines, but you still get the unwanted parentheses and quotes. So don't use pprint module. – smci Oct 02 '19 at 20:55
  • There are many related questions on string formatting for pretty-printing: [How to Print “Pretty” String Output in Python](https://stackoverflow.com/questions/5084743/how-to-print-pretty-string-output-in-python) and others. – smci Oct 02 '19 at 21:10
  • Possible duplicate of [How to Print "Pretty" String Output in Python](https://stackoverflow.com/questions/5084743/how-to-print-pretty-string-output-in-python) – smci Oct 04 '19 at 07:50
  • There are some good ideas [here](https://stackoverflow.com/q/30451252/4014959), but beware the Python 2 code in some of those answers. Eg, instead of `xrange` use `range` in Python 3. – PM 2Ring Oct 04 '19 at 08:22

2 Answers2

1

Try this out. You can tweak with various width values to suit your requirement.

>>> pp = pprint.PrettyPrinter(width=38, compact=True)
>>> pp.pprint(deck)
['AC', '2C', '3C', '4C', '5C', '6C',
 '7C', '8C', '9C', 'TC', 'QC', 'JC',
 'KC', 'AD', '2D', '3D', '4D', '5D',
 '6D', '7D', '8D', '9D', 'TD', 'QD',
 'JD', 'KD', 'AH', '2H', '3H', '4H',
 '5H', '6H', '7H', '8H', '9H', 'TH',
 'QH', 'JH', 'KH', 'AS', '2S', '3S',
 '4S', '5S', '6S', '7S', '8S', '9S',
 'TS', 'QS', 'JS', 'KS']
taurus05
  • 2,491
  • 15
  • 28
1

Lazy way, just print cards directly from within your nested loops:

>>> for s in ("CDHS"): 
...     for v in ("A23456789TQJK"): 
...         print(v+s, end=' ') 
...     print() 
...
AC 2C 3C 4C 5C 6C 7C 8C 9C TC QC JC KC 
AD 2D 3D 4D 5D 6D 7D 8D 9D TD QD JD KD 
AH 2H 3H 4H 5H 6H 7H 8H 9H TH QH JH KH 
AS 2S 3S 4S 5S 6S 7S 8S 9S TS QS JS KS

More Pythonic way, split your deck into chunks and tabulate:

>>> from tabulate import tabulate  # pip install tabulate
>>> from wimpy import chunks  # pip install wimpy
>>> deck2 = chunks(deck, len("A23456789TQJK"))
>>> print(tabulate(deck2, tablefmt="plain"))
AC  2C  3C  4C  5C  6C  7C  8C  9C  TC  QC  JC  KC
AD  2D  3D  4D  5D  6D  7D  8D  9D  TD  QD  JD  KD
AH  2H  3H  4H  5H  6H  7H  8H  9H  TH  QH  JH  KH
AS  2S  3S  4S  5S  6S  7S  8S  9S  TS  QS  JS  KS
wim
  • 338,267
  • 99
  • 616
  • 750