Re: dict comprehension question.

2013-01-01 Thread 88888 Dihedral
On Tuesday, January 1, 2013 11:10:48 AM UTC+8, Steven D'Aprano wrote: > On Sat, 29 Dec 2012 18:56:57 -0500, Terry Reedy wrote: > > > > > On 12/29/2012 2:48 PM, Quint Rankid wrote: > > > > > >> Given a list like: > > >> w = [1, 2, 3, 1, 2, 4, 4, 5, 6, 1] > > >> I would like to be able to do

Re: dict comprehension question.

2012-12-31 Thread Steven D'Aprano
On Sat, 29 Dec 2012 18:56:57 -0500, Terry Reedy wrote: > On 12/29/2012 2:48 PM, Quint Rankid wrote: > >> Given a list like: >> w = [1, 2, 3, 1, 2, 4, 4, 5, 6, 1] >> I would like to be able to do the following as a dict comprehension. >> a = {} >> for x in w: >> a[x] = a.get(x,0) + 1 >> resu

Re: dict comprehension question.

2012-12-29 Thread Joel Goldstick
On Sat, Dec 29, 2012 at 7:26 PM, Tim Chase wrote: > On 12/29/12 15:40, Mitya Sirenef wrote: > >> >>> w = [1,2,3,1,2,4,4,5,6,1] >>> >>> s = set(w) >>> >>> s >>> set([1, 2, 3, 4, 5, 6]) >>> >>> {x:w.count(x) for x in s} >>> {1: 3, 2: 2, 3: 1, 4: 2, 5: 1, 6: 1} >>>

Re: dict comprehension question.

2012-12-29 Thread Tim Chase
On 12/29/12 15:40, Mitya Sirenef wrote: >>> w = [1,2,3,1,2,4,4,5,6,1] >>> s = set(w) >>> s set([1, 2, 3, 4, 5, 6]) >>> {x:w.count(x) for x in s} {1: 3, 2: 2, 3: 1, 4: 2, 5: 1, 6: 1} Indeed, this is much better -- I didn't think of it.. Except that you're st

Re: dict comprehension question.

2012-12-29 Thread Terry Reedy
On 12/29/2012 2:48 PM, Quint Rankid wrote: Given a list like: w = [1, 2, 3, 1, 2, 4, 4, 5, 6, 1] I would like to be able to do the following as a dict comprehension. a = {} for x in w: a[x] = a.get(x,0) + 1 results in a having the value: {1: 3, 2: 2, 3: 1, 4: 2, 5: 1, 6: 1} Let me paraphr

Re: dict comprehension question.

2012-12-29 Thread Terry Reedy
On 12/29/2012 4:40 PM, Mitya Sirenef wrote: On 12/29/2012 03:15 PM, Joel Goldstick wrote: Would this help: >>> w = [1,2,3,1,2,4,4,5,6,1] >>> s = set(w) >>> s set([1, 2, 3, 4, 5, 6]) >>> {x:w.count(x) for x in s} {1: 3, 2: 2, 3: 1, 4: 2, 5: 1, 6: 1} >>> Indeed,

Re: dict comprehension question.

2012-12-29 Thread Mitya Sirenef
On 12/29/2012 03:15 PM, Joel Goldstick wrote: On Sat, Dec 29, 2012 at 3:09 PM, Mitya Sirenef > wrote: On 12/29/2012 03:01 PM, Mitya Sirenef wrote: On 12/29/2012 02:48 PM, Quint Rankid wrote: >> Newbie question. I've googled a little and haven

Re: dict comprehension question.

2012-12-29 Thread MRAB
On 2012-12-29 19:48, Quint Rankid wrote: Newbie question. I've googled a little and haven't found the answer. Given a list like: w = [1, 2, 3, 1, 2, 4, 4, 5, 6, 1] I would like to be able to do the following as a dict comprehension. a = {} for x in w: a[x] = a.get(x,0) + 1 results in a hav

Re: dict comprehension question.

2012-12-29 Thread Peter Otten
Quint Rankid wrote: > Newbie question. I've googled a little and haven't found the answer. > > Given a list like: > w = [1, 2, 3, 1, 2, 4, 4, 5, 6, 1] > I would like to be able to do the following as a dict comprehension. > a = {} > for x in w: > a[x] = a.get(x,0) + 1 > results in a having t

Re: dict comprehension question.

2012-12-29 Thread Joel Goldstick
On Sat, Dec 29, 2012 at 3:09 PM, Mitya Sirenef wrote: > On 12/29/2012 03:01 PM, Mitya Sirenef wrote: > >> On 12/29/2012 02:48 PM, Quint Rankid wrote: >> > >> Newbie question. I've googled a little and haven't found the answer. > >> > >> Given a list like: > >> w = [1, 2, 3, 1, 2, 4, 4, 5, 6, 1] >

Re: dict comprehension question.

2012-12-29 Thread Mitya Sirenef
On 12/29/2012 03:01 PM, Mitya Sirenef wrote: On 12/29/2012 02:48 PM, Quint Rankid wrote: >> Newbie question. I've googled a little and haven't found the answer. >> >> Given a list like: >> w = [1, 2, 3, 1, 2, 4, 4, 5, 6, 1] >> I would like to be able to do the following as a dict comprehension.

Re: dict comprehension question.

2012-12-29 Thread Mitya Sirenef
On 12/29/2012 02:48 PM, Quint Rankid wrote: Newbie question. I've googled a little and haven't found the answer. Given a list like: w = [1, 2, 3, 1, 2, 4, 4, 5, 6, 1] I would like to be able to do the following as a dict comprehension. a = {} for x in w: a[x] = a.get(x,0) + 1 results in a

Re: dict comprehension question.

2012-12-29 Thread Roy Smith
In article <724d4fea-606a-4503-b538-87442f6bc...@ci3g2000vbb.googlegroups.com>, Quint Rankid wrote: > Newbie question. I've googled a little and haven't found the answer. > > Given a list like: > w = [1, 2, 3, 1, 2, 4, 4, 5, 6, 1] > I would like to be able to do the following as a dict compre

RE: Dict comprehension help

2012-12-06 Thread Joseph L. Casale
>You could put the loop into a helper function, but if you are looping >through the same my_list more than once why not build a lookup table > >my_dict = {d["key"]: d for d in my_list} > >and then find the required dict with > >my_dict[value] I suppose, what I failed to clarify was that for each l

RE: Dict comprehension help

2012-12-06 Thread Peter Otten
Joseph L. Casale wrote: [Ian Kelly] >> {k: v for d in my_list if d['key'] == value for (k, v) in d.items()} > > Ugh, had part of that backwards:) Nice! > >> However, since you say that all dicts have a unique value for >> z['key'], you should never need to actually merge two dicts, correct? >> I

RE: Dict comprehension help

2012-12-05 Thread Joseph L. Casale
> {k: v for d in my_list if d['key'] == value for (k, v) in d.items()} Ugh, had part of that backwards:) Nice! > However, since you say that all dicts have a unique value for > z['key'], you should never need to actually merge two dicts, correct? > In that case, why not just use a plain for loop

Re: Dict comprehension help

2012-12-05 Thread Ian Kelly
On Wed, Dec 5, 2012 at 8:03 PM, Joseph L. Casale wrote: > I get a list of dicts as output from a source I need to then extract various > dicts > out of. I can easily extract the dict of choice based on it containing a key > with > a certain value using list comp but I was hoping to use dict comp

Re: Dict Comprehension ?

2008-10-07 Thread Ernst-Ludwig Brust
<[EMAIL PROTECTED]> schrieb im Newsbeitrag news:[EMAIL PROTECTED] > > from collections import defaultdict > da=defaultdict(int) > for x in [10]: >for y in [11]: >da[abs(x-y)]+=1 Thangs, collections are a real good idea. I will use this version. Ernsst-Ludwwig Brust -- http://mail.p

Re: Dict Comprehension ?

2008-10-07 Thread Ernst-Ludwig Brust
"Ben Finney" <[EMAIL PROTECTED]> schrieb im Newsbeitrag news:[EMAIL PROTECTED] > "Ernst-Ludwig Brust" <[EMAIL PROTECTED]> writes: > > So, generator expressions can be a powerful way to clarify the purpose > of a section of code. They can be over-used, though: don't use them > unless they actually

Re: Dict Comprehension ?

2008-10-07 Thread Ernst-Ludwig Brust
<[EMAIL PROTECTED]> schrieb im Newsbeitrag news:[EMAIL PROTECTED] > > Instead of creating the list (array) dif, you can create a lazy > iterator. Then you can fed it to a set. Thangs, this idea is not only less storage-consuming but even faster than the "List-Version". But, i used the idea of pru

Re: Dict Comprehension ?

2008-10-06 Thread Ben Finney
"Ernst-Ludwig Brust" <[EMAIL PROTECTED]> writes: > Given 2 Number-Lists say l0 and l1, > count the various positiv differences between the 2 lists > > the following part works: > > dif=[abs(x-y) for x in l0 for y in l1] > da={} > for d in dif: da[d]=da.get(d,0)+1 > > i wonder, if there is a way

Re: Dict Comprehension ?

2008-10-06 Thread pruebauno
On Oct 6, 8:59 am, "Ernst-Ludwig Brust" <[EMAIL PROTECTED]> wrote: > Given 2 Number-Lists say l0 and l1, > count the various positiv differences between the 2 lists > > the following part works: > > dif=[abs(x-y) for x in l0 for y in l1] > da={} > for d in dif: da[d]=da.get(d,0)+1 > > i wonder, if

Re: Dict Comprehension ?

2008-10-06 Thread bearophileHUGS
Ernst-Ludwig Brust: > Given 2 Number-Lists say l0 and l1, > count the various positiv differences between the 2 lists >... > i wonder, if there is a way, to avoid the list dif Instead of creating the list (array) dif, you can create a lazy iterator. Then you can fed it to a set. Bye, bearophile -

Re: dict comprehension

2008-02-03 Thread Arnaud Delobelle
On Feb 2, 9:10 pm, [EMAIL PROTECTED] wrote: > Steven Bethard: > > > It also doesn't build the unnecessary intermediate tuples: > > I see, but can't the interpreter improved to remove similar > intermediate tuples anyway? Do you mean the compiler? > Is this a difficult thing to do? Yes, due to th

Re: dict comprehension

2008-02-02 Thread bearophileHUGS
Steven Bethard: > It also doesn't build the unnecessary intermediate tuples: I see, but can't the interpreter improved to remove similar intermediate tuples anyway? Is this a difficult thing to do? Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: dict comprehension

2008-02-02 Thread Steven Bethard
Wildemar Wildenburger wrote: > Arnaud Delobelle wrote: >>> I believe both set and dict comprehensions will be in 3.0. >> >> Python 3.0a1+ (py3k:59330, Dec 4 2007, 18:44:39) >> [GCC 4.0.1 (Apple Inc. build 5465)] on darwin >> Type "help", "copyright", "credits" or "license" for more information. >>

Re: dict comprehension

2008-02-01 Thread Stefan Behnel
Wildemar Wildenburger wrote: > Arnaud Delobelle wrote: >>> I believe both set and dict comprehensions will be in 3.0. >> >> Python 3.0a1+ (py3k:59330, Dec 4 2007, 18:44:39) >> [GCC 4.0.1 (Apple Inc. build 5465)] on darwin >> Type "help", "copyright", "credits" or "license" for more information. >>

Re: dict comprehension

2008-02-01 Thread Wildemar Wildenburger
Arnaud Delobelle wrote: >> I believe both set and dict comprehensions will be in 3.0. > > Python 3.0a1+ (py3k:59330, Dec 4 2007, 18:44:39) > [GCC 4.0.1 (Apple Inc. build 5465)] on darwin > Type "help", "copyright", "credits" or "license" for more information. {x*x for x in range(10)} > {0, 1

Re: dict comprehension

2008-02-01 Thread bearophileHUGS
Paul McGuire: > Why not just > D = dict(zip(keys,values)) > ?? Because this may require less memory: from itertools import izip D = dict(izip(keys, values)) :-) Bear hugs, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: dict comprehension

2008-02-01 Thread Paul McGuire
On Feb 1, 12:13 am, Paddy <[EMAIL PROTECTED]> wrote: > On Feb 1, 6:06 am, "Ryan Ginstrom" <[EMAIL PROTECTED]> wrote: > > > > On Behalf Of Daniel Fetchinson > > > What does the author mean here? What's the Preferably One Way > > > (TM) to do something analogous to a dict comprehension? > > > I imagi

Re: dict comprehension

2008-01-31 Thread Arnaud Delobelle
On Feb 1, 6:21 am, "Terry Reedy" <[EMAIL PROTECTED]> wrote: > "Daniel Fetchinson" <[EMAIL PROTECTED]> wrote in message > > news:[EMAIL PROTECTED] > | Hi folks, > | > | There is a withdrawn PEP about a new syntax for dict comprehension: > |http://www.python.org/dev/peps/pep-0274/which says: > > I be

Re: dict comprehension

2008-01-31 Thread Terry Reedy
"Daniel Fetchinson" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] | Hi folks, | | There is a withdrawn PEP about a new syntax for dict comprehension: | http://www.python.org/dev/peps/pep-0274/ which says: I believe both set and dict comprehensions will be in 3.0. -- http://mai

Re: dict comprehension

2008-01-31 Thread Daniel Fetchinson
> > Hi folks, > > > > There is a withdrawn PEP about a new syntax for dict comprehension: > > http://www.python.org/dev/peps/pep-0274/ which says: > > > > "Substantially all of its benefits were subsumed by generator > > expressions coupled with the dict() constructor." > > > > What does the author

Re: dict comprehension

2008-01-31 Thread Paddy
On Feb 1, 6:06 am, "Ryan Ginstrom" <[EMAIL PROTECTED]> wrote: > > On Behalf Of Daniel Fetchinson > > What does the author mean here? What's the Preferably One Way > > (TM) to do something analogous to a dict comprehension? > > I imagine something like this: > > >>> keys = "a b c".split() > >>> valu

Re: dict comprehension

2008-01-31 Thread Paul Rubin
"Daniel Fetchinson" <[EMAIL PROTECTED]> writes: > What does the author mean here? What's the Preferably One Way (TM) to > do something analogous to a dict comprehension? from itertools import izip d = dict((k,v) for k,v in izip(keys, values)) -- http://mail.python.org/mailman/listinfo/python-list

Re: dict comprehension

2008-01-31 Thread Gary Herron
Ryan Ginstrom wrote: >> On Behalf Of Daniel Fetchinson >> What does the author mean here? What's the Preferably One Way >> (TM) to do something analogous to a dict comprehension? >> > > I imagine something like this: > > keys = "a b c".split() values = [1, 2, 3] D = dict([(a

Re: dict comprehension

2008-01-31 Thread Gary Herron
Daniel Fetchinson wrote: > Hi folks, > > There is a withdrawn PEP about a new syntax for dict comprehension: > http://www.python.org/dev/peps/pep-0274/ which says: > > "Substantially all of its benefits were subsumed by generator > expressions coupled with the dict() constructor." > > What does the

RE: dict comprehension

2008-01-31 Thread Ryan Ginstrom
> On Behalf Of Daniel Fetchinson > What does the author mean here? What's the Preferably One Way > (TM) to do something analogous to a dict comprehension? I imagine something like this: >>> keys = "a b c".split() >>> values = [1, 2, 3] >>> D = dict([(a, b) for a, b in zip(keys, values)]) >>> D {