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
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
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}
>>>
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
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
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,
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.ge
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
re
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.
ven 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:
> >>
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
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
re
e to do the following as a dict comprehension.
> a = {}
> for x in w:
> a[x] = a.get(x,0) + 1
Why are you trying to do this mind-blowing thing? Other than as an
entry in an obfuscated code contest, what is this for?
Anyway, I don't think this is possible with a dict comprehen
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:
>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
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
> {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
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
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 so the output
was not contained within a list.
reduce(lambda x,y:
<[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
"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
<[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
"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
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
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
-
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, to avoid the list dif
Ernst-Ludwig Brust
--
http://mail.python.o
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
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
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.
>>
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.
>>
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
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
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
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
"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
> > 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
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 some
"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.
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 =
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() c
> 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 =
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 mean here?
42 matches
Mail list logo