Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #7615 > unrolled thread
| Started by | Zachary Dziura <zcdziura@gmail.com> |
|---|---|
| First post | 2011-06-14 08:29 -0700 |
| Last post | 2011-06-15 10:08 +1000 |
| Articles | 8 — 7 participants |
Back to article view | Back to comp.lang.python
What is the Most Efficient Way of Printing A Dict's Contents Out In Columns? Zachary Dziura <zcdziura@gmail.com> - 2011-06-14 08:29 -0700
Re: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns? Terry Reedy <tjreedy@udel.edu> - 2011-06-14 12:06 -0400
Re: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns? Zach Dziura <zcdziura@gmail.com> - 2011-06-14 10:48 -0700
Re: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns? MRAB <python@mrabarnett.plus.com> - 2011-06-14 19:37 +0100
Re: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns? Terry Reedy <tjreedy@udel.edu> - 2011-06-15 00:53 -0400
Re: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns? Karim <karim.liateni@free.fr> - 2011-06-14 20:28 +0200
Re: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns? Ben Finney <ben+python@benfinney.id.au> - 2011-06-15 09:40 +1000
Re: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns? Chris Angelico <rosuav@gmail.com> - 2011-06-15 10:08 +1000
| From | Zachary Dziura <zcdziura@gmail.com> |
|---|---|
| Date | 2011-06-14 08:29 -0700 |
| Subject | What is the Most Efficient Way of Printing A Dict's Contents Out In Columns? |
| Message-ID | <22011833-0833-4589-8326-909f4c57f1aa@a10g2000vbz.googlegroups.com> |
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:
Header1 Header2 Header3 Header4
1 2 3 4
5 6 7 8
9 10 11 12
[toc] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2011-06-14 12:06 -0400 |
| Message-ID | <mailman.222.1308067590.11593.python-list@python.org> |
| In reply to | #7615 |
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:
>
> Header1 Header2 Header3 Header4
> 1 2 3 4
> 5 6 7 8
> 9 10 11 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 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
[toc] | [prev] | [next] | [standalone]
| From | Zach Dziura <zcdziura@gmail.com> |
|---|---|
| Date | 2011-06-14 10:48 -0700 |
| Message-ID | <312107af-ee77-42ff-a8c5-8c32bd2976cf@f11g2000vbx.googlegroups.com> |
| In reply to | #7618 |
> 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!
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2011-06-14 19:37 +0100 |
| Message-ID | <mailman.230.1308076649.11593.python-list@python.org> |
| In reply to | #7622 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2011-06-15 00:53 -0400 |
| Message-ID | <mailman.257.1308113598.11593.python-list@python.org> |
| In reply to | #7622 |
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
[toc] | [prev] | [next] | [standalone]
| From | Karim <karim.liateni@free.fr> |
|---|---|
| Date | 2011-06-14 20:28 +0200 |
| Message-ID | <mailman.228.1308076147.11593.python-list@python.org> |
| In reply to | #7615 |
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:
>
> Header1 Header2 Header3 Header4
> 1 2 3 4
> 5 6 7 8
> 9 10 11 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
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2011-06-15 09:40 +1000 |
| Message-ID | <8762o8t21e.fsf@benfinney.id.au> |
| In reply to | #7615 |
Zachary Dziura <zcdziura@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
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2011-06-15 10:08 +1000 |
| Message-ID | <mailman.242.1308096536.11593.python-list@python.org> |
| In reply to | #7645 |
On Wed, Jun 15, 2011 at 9:40 AM, Ben Finney <ben+python@benfinney.id.au> wrote: > Zachary Dziura <zcdziura@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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web