list_to_dict()
Peter Abel
p-abel at t-online.de
Sat Jan 18 14:49:22 EST 2003
Martin Maney <maney at pobox.com> wrote in message news:<b0aggh$e7m$4 at wheel2.two14.net>...
> Peter Abel <p-abel at t-online.de> wrote:
> > Sorry, when i didn't read the thread quite exactly,
> > but I think your basic problem is something like this, which can
> > be easily solved.
> > I'm using PYTHON 2.2.2:
> >>>> l1=['zero','one','two','three']
> >>>> l2=range(5)
> >>>> l1
> ['zero', 'one', 'two', 'three']
> >>>> l2
> [0, 1, 2, 3, 4]
> >>>> di=dict(zip(l1,l2))
> >>>> di
> > {'zero': 0, 'three': 3, 'two': 2, 'one': 1}
>
> It is something like that, but the difference is sort of crucial. For
> the dictionary in your example, the source would be the single sequence
>
> (('zero',0),('one',1),('two',2)('three',3))
>
> Except that in practice, the key may be more than one field of the
> nested sequences, as may the value. So this doesn't reduce to a simple
> dict(zip(...)) for any simple '...'
>
> Besides, the dict() constructor seems not to exist prior to 2.2, and at
> this time I still need most of my code to coexist with 2.1.
> Furthermore, when I ran some simple benchmarks, calling a separate
> function ran significantly faster than an inline dict(zip(...)) for the
> small (five to ten items) cases I'm most frequently building. I think
> I emailed the test proggy home before I left the lab - have to look for
> it tomorrow.
>
> Here's the bare sketch:
>
> def dzip(keys, values):
> res = {}
> for k,v in zip(keys, values):
> res[k] = v
> return res
>
> foo = dzip(field_names, field_values)
>
> runs faster than
>
> foo = dict(zip(field_names, field_values))
>
> for cases where there are a few to a few tens of names & values. Hmmm,
> just occurred to me that, although I tried making local bindings for
> other things, I never did that to dict or zip...
>
> Okay, definitely need to repoen this can of worms... later.
Well, in order not to post some stupid thoughts I read the whole thread again,
and I think having understood that there may be 2 basic problems:
1) to generate the keys and the values by a special algo what is
nicely solved by Martin v. Löwis in my opinion
2) optimizing for time as you showed with the dzip-function
which can be optimized with some effort.
e.g.:
>>> def myzip(keys,values):
... res={}
... map(lambda x,y:res.setdefault(x,y),keys,values)
... return res
And I think it goes with python 2.1. or earlier.
A little time_test(number_of_keys) - function which generates
simple 5-random-letters-keys and a value-list as range(number_of_keys)
will show an increase in speed.
>>> time_test(50000)
dzip : 0.28100001812
myzip : 0.140999913216
>>> time_test(500000)
dzip : 3.31200003624
myzip : 1.59399998188
Peter
PS. Though I like the map-function, because it's short and pithy,
I don't like it very much in situations like this, because it's
used like a procedure (as in pascal or fortran), but it's a function
that returns a list, which is thrown in the wind.
More information about the Python-list
mailing list