1

I need to dynamically create a dict of dicts and then print them in a neat table in the terminal. Eventually, i'm going to be adding to each dict and re-printing the table.

Here's my code so far:

def handy(self, play_deck):
    a = raw_input('How many hands? ')
    for i in range(int(a)):
        h = "hand" + str(i+ 1) # dynamilly create key
        q, play_deck = self.deal(self.full_deck) # pull cards from deck
        self.dict_of_hands.setdefault(h, q) # load up dict of hands

    hand_keys = self.dict_of_hands.keys()
    the_hands = self.dict_of_hands
    print "\n"
    for hand in hand_keys:
        for card in range(len(the_hands[hand].keys())):
            print hand
            print the_hands[hand][card]

and my output is this:

How many hands? 4

hand4
6 of Clubs
hand4
4 of Diamonds
hand1
8 of Diamonds
hand1
10 of Hearts
hand2
10 of Hearts
hand2
5 of Clubs
hand3
9 of Diamonds
hand3
3 of Hearts

I'd like to have:

hand1           hand2           hand3            hand4
2 of hearts     8 of spades     ace of clubs     jack of diomonds
5 of diamonds   ...             ...              ...etc.

The examples i've seen on SO are based on a known number of columns. This needs to be dynamic.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
DBWeinstein
  • 8,605
  • 31
  • 73
  • 118
  • possible duplicate of [Python: pretty-printing ascii tables?](http://stackoverflow.com/questions/5909873/python-pretty-printing-ascii-tables) – ecatmur Aug 22 '12 at 18:01
  • @ecatmur the libraries referenced in the answers seem to require an known number of columns (as opposed to dynamically responding to the number of columns). Am I just being dense? – DBWeinstein Aug 22 '12 at 18:23
  • Everything in Python is dynamic. You just need to convert your data into a list of rows. – ecatmur Aug 22 '12 at 18:26

3 Answers3

1

To convert into a list of rows, use zip:

header = dict_of_hands.keys()
rows = zip(*dict_of_hands.items())

import texttable
table = texttable.Texttable()
table.header(header)
table.add_rows(rows, header=False)
print table.draw()
ecatmur
  • 152,476
  • 27
  • 293
  • 366
  • @dwstein no, it's on PyPi at http://pypi.python.org/pypi?name=texttable&%3aaction=display. Other pretty-table packages and recipes have a similar interface if you can't use that one. – ecatmur Aug 22 '12 at 20:53
  • I'm getting an error: "'module' object has no attribute 'Table'. The line should read `table = texttable.Texttable()` That got a table printed at least. I just need to work on the info now. – DBWeinstein Aug 22 '12 at 20:53
1

OK. Taking what @ecatmur provided, I now have the following, which seems to work for now.

def handy(self, play_deck):
    a = raw_input('How many hands? ')
    for i in range(int(a)):
        h = "hand" + str(i+ 1) # dynamilly create key
        q, play_deck = self.deal(self.full_deck) # pull cards from deck
        self.dict_of_hands.setdefault(h, q) # load up dict of hands

    hand_keys = self.dict_of_hands.keys()
    the_hands = self.dict_of_hands

    first_cards = []
    second_cards = []
    for hand in the_hands:
        # print the_hands[hand][0]
        first_cards.append(the_hands[hand][0])

    for hand in the_hands:
        # print the_hands[hand][1]
        second_cards.append(the_hands[hand][1])         

    header = self.dict_of_hands.keys()


    table = texttable.Texttable()
    table.header(header)
    table.add_rows([first_cards, second_cards], header=False)
    print table.draw()

and the out put is:

How many hands? 4
+---------------+---------------+------------------+---------------+
|     hand4     |     hand1     |      hand2       |     hand3     |
+===============+===============+==================+===============+
| Jack of Clubs | 9 of Diamonds | Jack of Diamonds | 4 of Spades   |
+---------------+---------------+------------------+---------------+
| 6 of Diamonds | 10 of Spades  | 9 of Diamonds    | Ace of Hearts |
+---------------+---------------+------------------+---------------+

The last piece to the puzzle will be making the following line dynamic:

table.add_rows([first_cards, second_cards], header=False)

Eventually, I'll need each hand to be of varying length.

DBWeinstein
  • 8,605
  • 31
  • 73
  • 118
0

Look at this recipe: Render tables for text interface

Alexey Kachayev
  • 6,106
  • 27
  • 24
  • Thanks for the info! My problem, however, is translating that example into a dynamic situation; i.e. the number and names of columns are going to vary. – DBWeinstein Aug 22 '12 at 20:21