Em Qui, 2006-03-23 às 21:23 -0800, Adam DePrince escreveu:
Wait! It occured to me. Why are we touching the key at all. This is
a dictionary with mutable values.
This idea occured to me but I always forget about the [:] trick, so I
didn't try it :).
Now, we *know* that all of the values are
Droppings from other timing tests; starbucks was kicking me out and I
was in a hurry.
Cheers - Adam DePrince
--
http://mail.python.org/mailman/listinfo/python-list
As long as you are optimizing, addition is slightly faster than
multiplication:
$ python2.4 -mtimeit 'h=1;h*=2'
100 loops, best of 3: 0.286 usec per loop
$ python2.4 -mtimeit 'h=1;h=h+h'
100 loops, best of 3: 0.23 usec per loop
Of course, that's only a 20% decrease, so it might not be
Say I have a dictionary like below:
d = {(100,500):[5,5], (100,501):[6,6], (100,502):[7,7]}
Say I want to multiply all the values of the dictionary by 2:
for key in d.keys():
d[key] = map(lambda x: x*2, d.get(key))
Is there a better/faster/cleaner way to achieve this ?
Thanks,
John
--
Em Sex, 2006-03-24 às 11:04 +1000, John McMonagle escreveu:
Is there a better/faster/cleaner way to achieve this ?
Maybe...
for key in d:
d[key] = [x*2 for x in d[key]]
...?
I can't thing of anything better :(...
HTH,
--
Felipe.
--
John McMonagle wrote:
Say I have a dictionary like below:
d = {(100,500):[5,5], (100,501):[6,6], (100,502):[7,7]}
Say I want to multiply all the values of the dictionary by 2:
for key in d.keys():
d[key] = map(lambda x: x*2, d.get(key))
Is there a better/faster/cleaner way to achieve
for key in d:
d[key] = [x*2 for x in d[key]]
Naw, if you are going to use list interpolation go all the way and save
yourself all of that ugly indexing into the dict.
d = {(100,500):[5,5], (100,501):[6,6], (100,502):[7,7]}
d.update( [[key,[x*2 for x in item]] for key,item in d.items()]
Em Qui, 2006-03-23 às 17:54 -0800, Scott David Daniels escreveu:
John McMonagle wrote:
Say I have a dictionary like below:
d = {(100,500):[5,5], (100,501):[6,6], (100,502):[7,7]}
Say I want to multiply all the values of the dictionary by 2:
for key in d.keys():
d[key] =
Em Qui, 2006-03-23 às 18:01 -0800, [EMAIL PROTECTED] escreveu:
for key in d:
d[key] = [x*2 for x in d[key]]
Naw, if you are going to use list interpolation go all the way and save
yourself all of that ugly indexing into the dict.
d = {(100,500):[5,5], (100,501):[6,6],
Yes, I cede that explicit indexing is faster by quite a bit.
There is a somewhat philosophical decision for why I avoided that. I
prefer to try to present python with as big of a picture of what I want
as possiable. update tells python what I want to do, whereas the
for-loop describes how to.
Wait! It occured to me. Why are we touching the key at all. This is
a dictionary with mutable values.
[EMAIL PROTECTED] ~]$ python2.4 -mtimeit -s 'd = {(100,500):[5,5],
(100,501):[6,6],
(100,502):[7,7]}; x = dict(d)' 'for i in x.values():
i[:]=[j*1 for j in i]'
10 loops, best of 3: 2.79
Excuse me, I mean
python2.4 -mtimeit -s 'from numarray import array; d =
{(100,500):[5,5], (100,501):[6,6],
(100,502):[7,7]}; x = dict(d);' 'for i in x.values():
i[0]*=1;i[1]*=1'
100 loops, best of 3: 1.72 usec per loop
i[0]*=1, not j[0]*=1 ...
--
12 matches
Mail list logo