Re: [Tutor] list of dicts <-> dict of lists?

2010-05-29 Thread Matthew Wood
On Fri, May 28, 2010 at 6:55 PM, Steven D'Aprano wrote: > On Fri, 28 May 2010 12:00:46 pm Matthew Wood wrote: > > > I THOUGHT the guaranteed same-ordering of dict.keys and dict.values > > started in python 2.6. That was a simple mistake. > > > > It turns out, that's not the case. But in general,

Re: [Tutor] list of dicts <-> dict of lists?

2010-05-28 Thread Luke Paireepinart
> I'm new to python, so i don't know if this is important, or what it means at > all.  I looked in setup.py, and it didn't tell me anything.  What does it > mean by "the necessary bits" were not found? Not really sure, but in the future please create a new e-mail to tutor@python.org rather than

Re: [Tutor] list of dicts <-> dict of lists?

2010-05-28 Thread Karl Jansson
I was trying to build python, and this printed to the terminal: Python build finished, but the necessary bits to build these modules were not found: _gdbm ossaudiodevreadline spwd To find the necessary bits, look in se

Re: [Tutor] list of dicts <-> dict of lists?

2010-05-28 Thread Steven D'Aprano
On Fri, 28 May 2010 12:00:46 pm Matthew Wood wrote: > I THOUGHT the guaranteed same-ordering of dict.keys and dict.values > started in python 2.6. That was a simple mistake. > > It turns out, that's not the case. But in general, access to dicts > and sets is unordered, so you can't/don't/shouldn

Re: [Tutor] list of dicts <-> dict of lists?

2010-05-28 Thread Peter Otten
David Perlman wrote: > Oh, except one problem: the csv.DictWriter lets you tell it what order > you want the columns output in. With your version, they just show up > in whatever order Python wants them. That's not hard to fix: >>> fieldnames = "abca" >>> cols = [data[fn] for fn in fieldnames]

Re: [Tutor] list of dicts <-> dict of lists?

2010-05-28 Thread David Perlman
Oh, except one problem: the csv.DictWriter lets you tell it what order you want the columns output in. With your version, they just show up in whatever order Python wants them. On May 28, 2010, at 2:33 AM, Peter Otten wrote: I think it's simpler and therefore more appropriate to use a norm

Re: [Tutor] list of dicts <-> dict of lists?

2010-05-28 Thread David Perlman
Aha, now this is the clever solution that I didn't find "outside the box". :) On May 28, 2010, at 2:33 AM, Peter Otten wrote: I think it's simpler and therefore more appropriate to use a normal csv.writer here: import csv import sys data = {'a': [1, 2, 3], 'c': [7, 8, 9], 'b': [4, 5, 6]} w

Re: [Tutor] list of dicts <-> dict of lists?

2010-05-28 Thread Sander Sweers
2010/5/28 spir ☣ : > his is a different feature from preserving *input* order of of keys, or of > key:value pairs. In Python 2.7 and 3.1 [1] we now have the OrderedDict which does preserve input order. Greets Sander [1] http://www.python.org/dev/peps/pep-0372/ __

Re: [Tutor] list of dicts <-> dict of lists?

2010-05-28 Thread spir ☣
On Thu, 27 May 2010 20:00:46 -0600 Matthew Wood wrote: > I THOUGHT the guaranteed same-ordering of dict.keys and dict.values started > in python 2.6. That was a simple mistake. > > It turns out, that's not the case. But in general, access to dicts and sets > is unordered, so you can't/don't/sh

Re: [Tutor] list of dicts <-> dict of lists?

2010-05-28 Thread Peter Otten
David Perlman wrote: > Using the csv.DictReader and csv.DictWriter lets you read and write > lists of dictionaries from files containing tabular data. I have a > system that naturally generates tabular data in the form of a > dictionary of lists: the dictionary key is the name of the column, and

Re: [Tutor] list of dicts <-> dict of lists?

2010-05-27 Thread Matthew Wood
Wow. Something horrible happened here. http://xkcd.com/386/ I THOUGHT the guaranteed same-ordering of dict.keys and dict.values started in python 2.6. That was a simple mistake. It turns out, that's not the case. But in general, access to dicts and sets is unordered, so you can't/don't/shoul

Re: [Tutor] list of dicts <-> dict of lists?

2010-05-27 Thread Steven D'Aprano
On Fri, 28 May 2010 09:44:36 am Matthew Wood wrote: > That said, the version with an extra line will work on python < 2.6, > so I'd probably just leave it that way. Why? That's like saying: "I could write y = x+2 in Python, but y = 1+x+1 will work too, so I'll write that instead, just in case.

Re: [Tutor] list of dicts <-> dict of lists?

2010-05-27 Thread Steven D'Aprano
On Fri, 28 May 2010 09:19:20 am Matthew Wood wrote: > I BELIEVE there's some new cool features in 2.6 or maybe 3.0 where > non-simultaneous access to my_dict.keys() and my_dict.values() will > keep them "paired up", but I don't know the details. This is not a new feature, but a very old feature.

Re: [Tutor] list of dicts <-> dict of lists?

2010-05-27 Thread Matthew Wood
Well, that makes a lot of sense. I probably should have looked it up. :-) That said, the version with an extra line will work on python < 2.6, so I'd probably just leave it that way. But thanks the docs pointer. Always useful. That said, if I KNEW that my software was only to be implemented

Re: [Tutor] list of dicts <-> dict of lists?

2010-05-27 Thread Mark Lawrence
I confess that I don't like top posting :) Please see below. On 28/05/2010 00:19, Matthew Wood wrote: #!/usr/bin/env python Here's my best attempt. I'm not sure if it's "simpler" than yours, but for me it seems a bit cleaner. Then again, I LOVE the zip operator, and the '*' operator too. :

Re: [Tutor] list of dicts <-> dict of lists?

2010-05-27 Thread Matthew Wood
#!/usr/bin/env python Here's my best attempt.  I'm not sure if it's "simpler" than yours, but for me it seems a bit cleaner.  Then again, I LOVE the zip operator, and the '*' operator too.  :-)  Whenever I see a "transpose this" type problem, I think zip. y = {'a': [1, 2, 3], 'c': [7, 8, 9], 'b

[Tutor] list of dicts <-> dict of lists?

2010-05-27 Thread David Perlman
Using the csv.DictReader and csv.DictWriter lets you read and write lists of dictionaries from files containing tabular data. I have a system that naturally generates tabular data in the form of a dictionary of lists: the dictionary key is the name of the column, and then the value is a li