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 the following as a

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 results in a having

dict comprehension question.

2012-12-29 Thread Quint Rankid
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 the value: {1: 3, 2: 2, 3: 1, 4: 2, 5:

Re: dict comprehension question.

2012-12-29 Thread Roy Smith
In article 724d4fea-606a-4503-b538-87442f6bc...@ci3g2000vbb.googlegroups.com, Quint Rankid qbr...@gmail.com 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

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 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. a = {}

Re: dict comprehension question.

2012-12-29 Thread Joel Goldstick
On Sat, Dec 29, 2012 at 3:09 PM, Mitya Sirenef msire...@lightbird.netwrote: 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,

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 the

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

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 msire...@lightbird.net mailto:msire...@lightbird.net 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

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, this is much

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

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 still

Re: dict comprehension question.

2012-12-29 Thread Joel Goldstick
On Sat, Dec 29, 2012 at 7:26 PM, Tim Chase python.l...@tim.thechases.comwrote: 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}