What is the Most Efficient Way of Printing A Dict's Contents Out In Columns?

2011-06-14 Thread Zachary Dziura
I have a dict that I would like to print out in a series of columns,
rather than as a bunch of lines. Normally when you do print(dict), the
output will look something like this:

{'Header2': ['2', '5', '8'], 'Header3': ['3', '6', '9'], 'Header1':
['1', '4', '7'], 'Header4': ['10', '11', '12']}

I can then iterate through (in this case) a list of the headers in
order to produce something similar to this:

Header1 = ['1', '4', '7']
Header2 = ['2', '5', '8']
Header3 = ['3', '6', '9']
Header4 = ['10', '11', '12']

What I want to know is how I can print out that information in a
column, where the header is the first line of the column, with the
data following underneath, like so:

Header1Header2Header3Header4
1  2  3   4
5  6  7   8
9  1011 12
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns?

2011-06-14 Thread Terry Reedy

On 6/14/2011 11:29 AM, Zachary Dziura wrote:

I have a dict that I would like to print out in a series of columns,
rather than as a bunch of lines. Normally when you do print(dict), the
output will look something like this:

{'Header2': ['2', '5', '8'], 'Header3': ['3', '6', '9'], 'Header1':
['1', '4', '7'], 'Header4': ['10', '11', '12']}

I can then iterate through (in this case) a list of the headers in
order to produce something similar to this:

Header1 = ['1', '4', '7']
Header2 = ['2', '5', '8']
Header3 = ['3', '6', '9']
Header4 = ['10', '11', '12']

What I want to know is how I can print out that information in a
column, where the header is the first line of the column, with the
data following underneath, like so:

Header1Header2Header3Header4
1  2  3   4
5  6  7   8
9  1011 12


You did not specify how much can be assumed about the dict and built in 
to the program and how much needs to be discovered with code. Assuming 
that this is not homework, here is a start:


d={'Header2': ['2', '5', '8'], 'Header3': ['3', '6', '9'],
   'Header1': ['1', '4', '7'], 'Header4': ['10', '11', '12']}

arr = []
for key,value in d.items():
line = ['{:10s}'.format(key)]
for num in value:
line.append('{:10s}'.format(num))
arr.append(line)

for line in zip(*arr):
for item in line:
print(item, end='')
print() # newline

   Header2   Header3   Header1   Header4
 2 3 110
 5 6 411
 8 9 712

For zip(*arr) to work properly, each line of arr should have the same 
length, which means that either each value of d has the same length or 
that you find the max length and pad lines with blanks up to the max 
length. The code above assumes the first.


If the items in each value of d are not strings, more fiddling is 
needed. The printed field size is also arbitrary. It needs adjusting for 
the actual max length. You might want to adjust it for each key-value 
pair in the dict, which is to say, each column of the resulting table.


--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


Re: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns?

2011-06-14 Thread Zach Dziura
 d={'Header2': ['2', '5', '8'], 'Header3': ['3', '6', '9'],
     'Header1': ['1', '4', '7'], 'Header4': ['10', '11', '12']}

 arr = []
 for key,value in d.items():
      line = ['{:10s}'.format(key)]
      for num in value:
          line.append('{:10s}'.format(num))
      arr.append(line)

 for line in zip(*arr):
      for item in line:
          print(item, end='')
      print() # newline
  
     Header2   Header3   Header1   Header4
           2         3         1        10
           5         6         4        11
           8         9         7        12

 For zip(*arr) to work properly, each line of arr should have the same
 length, which means that either each value of d has the same length or
 that you find the max length and pad lines with blanks up to the max
 length. The code above assumes the first.

 If the items in each value of d are not strings, more fiddling is
 needed. The printed field size is also arbitrary. It needs adjusting for
 the actual max length. You might want to adjust it for each key-value
 pair in the dict, which is to say, each column of the resulting table.

 --
 Terry Jan Reedy

I just have one quick question. On the line where you have zip(*arr),
what is the * for? Is it like the pointer operator, such as with C? Or
is it exactly the pointer operator?

Otherwise, thank you for the example! This isn't homework, but I'm
working on something at work, and I was wondering how to properly
format the output from CSV files into another file. It's all a part of
an analyzer script for database tables, and the table wherein. Thank
you a bunch for the help!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns?

2011-06-14 Thread Karim

On 06/14/2011 05:29 PM, Zachary Dziura wrote:

I have a dict that I would like to print out in a series of columns,
rather than as a bunch of lines. Normally when you do print(dict), the
output will look something like this:

{'Header2': ['2', '5', '8'], 'Header3': ['3', '6', '9'], 'Header1':
['1', '4', '7'], 'Header4': ['10', '11', '12']}

I can then iterate through (in this case) a list of the headers in
order to produce something similar to this:

Header1 = ['1', '4', '7']
Header2 = ['2', '5', '8']
Header3 = ['3', '6', '9']
Header4 = ['10', '11', '12']

What I want to know is how I can print out that information in a
column, where the header is the first line of the column, with the
data following underneath, like so:

Header1Header2Header3Header4
1  2  3   4
5  6  7   8
9  1011 12
Over alternative that only costs 2 lines of code, use pretty print (not 
in columns but crystal clear):


import pprint
pprint.pprint(my_dict)

or in a file:
pprint.pprint(my_dict, open(output.dat, wb))

Cheers
karim


--
http://mail.python.org/mailman/listinfo/python-list


Re: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns?

2011-06-14 Thread MRAB

On 14/06/2011 18:48, Zach Dziura wrote:
[snip]

I just have one quick question. On the line where you have zip(*arr),
what is the * for? Is it like the pointer operator, such as with C? Or
is it exactly the pointer operator?


[snip]
The * in the argument list of a function call unpacks the following
list as arguments for the call, for example, zip(*[0, 1, 2]) becomes
zip(0, 1, 2), so zip(*arr) becomes zip(arr[0], arr[1], ...).

There's also **, which unpacks a dict as keyword arguments.
--
http://mail.python.org/mailman/listinfo/python-list


Re: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns?

2011-06-14 Thread Ben Finney
Zachary Dziura zcdzi...@gmail.com writes:

 What I want to know is how I can print out that information in a
 column, where the header is the first line of the column, with the
 data following underneath, like so:

I'm glad you got some good replies. It probably reflects badly on me
that my first thought was URL:http://bash.org/?5804.

-- 
 \  “In case of fire, do your utmost to alarm the porter.” —hotel, |
  `\Vienna |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns?

2011-06-14 Thread Chris Angelico
On Wed, Jun 15, 2011 at 9:40 AM, Ben Finney ben+pyt...@benfinney.id.au wrote:
 Zachary Dziura zcdzi...@gmail.com writes:

 What I want to know is how I can print out that information in a
 column, where the header is the first line of the column, with the
 data following underneath, like so:

 I'm glad you got some good replies. It probably reflects badly on me
 that my first thought was URL:http://bash.org/?5804.

Well *OBVIOUSLY* the difference is that that snippet is referring to
Ms Access, and on this list we're working with Montgomery Python,
and as we all know, women simply cannot do these things.

Chris Angelico
/me ducks the slings and arrows of outrageous sexism
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns?

2011-06-14 Thread Terry Reedy

On 6/14/2011 2:37 PM, MRAB wrote:

On 14/06/2011 18:48, Zach Dziura wrote:
[snip]

I just have one quick question. On the line where you have zip(*arr),
what is the * for? Is it like the pointer operator, such as with C? Or
is it exactly the pointer operator?


[snip]
The * in the argument list of a function call unpacks the following
list as arguments for the call, for example, zip(*[0, 1, 2]) becomes
zip(0, 1, 2), so zip(*arr) becomes zip(arr[0], arr[1], ...).

There's also **, which unpacks a dict as keyword arguments.


* and ** in a function call, which distribute arguments,
are essentially the inverse of * and ** in function definitions,
where they say to collect arguments.

--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list