slais-www:
> Slower than
> ...
Okay, I seen there's a little confusion, I try to say it more clearly.
Generally this is the faster version (faster than the version with
get), especially if you use Psyco:
version 1)
if 'B' in counter:
counter['B'] += 1
else:
counter['B'] = 1
[EMAIL PROTECTED] wrote:
Marc 'BlackJack' Rintsch:
counter['B'] = counter.get('B', 0) + 1
If you benchmark it, you will find that using the get() method it's
quite slower.
Slower than
if 'B' in counter:
> counter['B'] += 1
> else:
> counter['B'] = 1
?
It is not slower than default
Frank Niemeyer wrote:
>
>> However incrementing a non-existing key throws an exception.
>
> Right. And that's exactly what I would expect, according to the
> "principle of least surprise" Python tries to obey. There's simply no
> way to increment a non-existent value - not without performing s
Marc 'BlackJack' Rintsch:
> counter['B'] = counter.get('B', 0) + 1
If you benchmark it, you will find that using the get() method it's
quite slower.
Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list
[EMAIL PROTECTED] schrieb:
> Frank Niemeyer:
>> There's simply no
>> way to increment a non-existent value - not without performing some
>> obscure implict behind-the-scenes stuff.
>
> Like importing and using a defaultdict(int).
There's nothing implicit in explicitly defining some default behavi
On Wed, 22 Oct 2008 10:12:56 -0700, bearophileHUGS wrote:
> Frank Niemeyer:
>> There's simply no
>> way to increment a non-existent value - not without performing some
>> obscure implict behind-the-scenes stuff.
>
> Like importing and using a defaultdict(int).
>
>
>> > So you
>> > either have t
Frank Niemeyer:
> There's simply no
> way to increment a non-existent value - not without performing some
> obscure implict behind-the-scenes stuff.
Like importing and using a defaultdict(int).
> > So you
> > either have to use a workaround:
>
> > >>> try:
> > ... counter['B'] += 1
> > ... ex
> However incrementing a non-existing key throws an exception.
Right. And that's exactly what I would expect, according to the
"principle of least surprise" Python tries to obey. There's simply no
way to increment a non-existent value - not without performing some
obscure implict behind-the-scenes
paul wrote:
Pat schrieb:
I know it's not "fair" to compare language features, but it seems to
me (a Python newbie) that appending a new key/value to a dict in
Python is awfully cumbersome.
In Python, this is the best code I could come up with for adding a new
key, value to a dict
mytable.s
Kirk Strauser wrote:
While we're on
the subject, use keyword arguments to dict like:
foo.update(dict(quux='blah', baz='bearophile', jdd='dict'))
was *much* slower, at 11.8s.
Presumably you would save half of that time by writing simply
foo.update(quux='blah', baz='bearophile', jdd
On Oct 13, 9:41 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> On Mon, 13 Oct 2008 14:10:43 +0200, Mathias Frey wrote:
> > However incrementing a non-existing key throws an exception. So you
> > either have to use a workaround:
>
> > >>> try:
> > ... counter['B'] += 1
> > ... except K
At 2008-10-13T13:14:15Z, [EMAIL PROTECTED] writes:
> jdd:
>> foo = {'bar': 'baz'}
>> foo.update({'quux': 'blah'})
>
> That creates a new dict, to throw it away. Don't do that.
I use that if I'm changing many values at once, eg:
foo.update({
'quux': 'blah',
'baz' : 'bearophile
[EMAIL PROTECTED] a écrit :
jdd:
foo = {'bar': 'baz'}
foo.update({'quux': 'blah'})
That creates a new dict, to throw it away.
Just to make it clear for the easily confused ones (like me...):
bearophile is talking about the dict passed as an argument to foo.update
- not about the behaviour
On Mon, 13 Oct 2008 14:10:43 +0200, Mathias Frey wrote:
> However incrementing a non-existing key throws an exception. So you
> either have to use a workaround:
>
> >>> try:
> ... counter['B'] += 1
> ... except KeyError:
> ... counter['B'] = 1
>
> Since this looks ugly somebody invented the
jdd:
> foo = {'bar': 'baz'}
> foo.update({'quux': 'blah'})
That creates a new dict, to throw it away. Don't do that. Use the
standard and more readable syntax:
> foo = {...}
> foo['quux'] = 'blah'
Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list
In Python, this is the best code I could come up with for
adding a new key, value to a dict
mytable.setdefault( k, [] ).append( v )
Naturally, right after writing my post I found that there is
an easier way:
table[ k ] = v
Just to be clear...these do two VERY different things:
>>> v1=42
Pat wrote:
I know it's not "fair" to compare language features, but it seems to me
(a Python newbie) that appending a new key/value to a dict in Python is
awfully cumbersome.
In Python, this is the best code I could come up with for adding a new
key, value to a dict
mytable.setdefault( k, [
Pat schrieb:
I know it's not "fair" to compare language features, but it seems to me
(a Python newbie) that appending a new key/value to a dict in Python is
awfully cumbersome.
In Python, this is the best code I could come up with for adding a new
key, value to a dict
mytable.setdefault( k,
On Oct 13, 7:21 am, Pat <[EMAIL PROTECTED]> wrote:
> Is there a better/easier way to code this in Python than the
> obtuse/arcane setdefault code?
foo = {'bar': 'baz'}
foo.update({'quux': 'blah'})
--
http://mail.python.org/mailman/listinfo/python-list
Pat wrote:
I know it's not "fair" to compare language features, but it seems to me
(a Python newbie) that appending a new key/value to a dict in Python is
awfully cumbersome.
In Python, this is the best code I could come up with for adding a new
key, value to a dict
mytable.setdefault( k, [
20 matches
Mail list logo