473,388 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

How to say $a=$b->{"A"} ||={} in Python?

Hi All.

I'd like to do the following in more succint code:

if k in b:
a=b[k]
else:
a={}
b[k]=a

a['A']=1
In perl it is just one line: $a=$b->{"A"} ||={}.

Thanks,
Geoffrey

Aug 16 '07 #1
18 1775
beginner <zy*******@gmail.comwrote:
I'd like to do the following in more succint code:

if k in b:
a=b[k]
else:
a={}
b[k]=a
b.setdefault(k, a)

--
Lawrence, oluyede.org - neropercaso.it
"It is difficult to get a man to understand
something when his salary depends on not
understanding it" - Upton Sinclair
Aug 16 '07 #2
On Aug 16, 5:43 pm, ra...@dot.com (Lawrence Oluyede) wrote:
beginner <zyzhu2...@gmail.comwrote:
I'd like to do the following in more succint code:
if k in b:
a=b[k]
else:
a={}
b[k]=a

b.setdefault(k, a)

--
Lawrence, oluyede.org - neropercaso.it
"It is difficult to get a man to understand
something when his salary depends on not
understanding it" - Upton Sinclair
I am afraid it is not the same. b.setdefault(k, {}) will always create
an empty dict, even if k is in b, as demonstrated in the below code.

b={}
def f(i):
print "I am evaluated %d" % i
return i

b.setdefault('A', f(1))
b.setdefault('A', f(2))
b
Aug 16 '07 #3
On Aug 16, 6:35 pm, beginner <zyzhu2...@gmail.comwrote:
Hi All.

I'd like to do the following in more succint code:

if k in b:
a=b[k]
else:
a={}
b[k]=a

a['A']=1

In perl it is just one line: $a=$b->{"A"} ||={}.

Thanks,
Geoffrey
Define b as a default dict:

b = defaultdict(dict)
b[k]['A'] = l

Carl Banks

Aug 16 '07 #4
On Aug 16, 3:35 pm, beginner <zyzhu2...@gmail.comwrote:
>
In perl it is just one line: $a=$b->{"A"} ||={}.
a = b.setdefault('A', {})

This combines all two actions together:
- Sets b['A'] to {} if it is not already defined
- Assigns b['A'] to a

More info on dict methods here:

http://docs.python.org/lib/typesmapping.html

Aug 17 '07 #5
On Aug 16, 8:28 pm, Jonathan Gardner
<jgardner.jonathangardner....@gmail.comwrote:
On Aug 16, 3:35 pm, beginner <zyzhu2...@gmail.comwrote:
In perl it is just one line: $a=$b->{"A"} ||={}.

a = b.setdefault('A', {})

This combines all two actions together:
- Sets b['A'] to {} if it is not already defined
- Assigns b['A'] to a

More info on dict methods here:

http://docs.python.org/lib/typesmapping.html
No, this has already been proposed and discarded. The OP does NOT
want this, because it always generates an empty {} whether it is
needed or not. Not really a big hardship, but if the default value
were some expensive-to-construct container class, then you would be
creating one every time you wanted to reference a value, on the chance
that the key did not exist.

Carl Banks' post using defaultdict is the correct solution. The
raison d'etre for defaultdict, and the reason that it is the solution
to the OP's question, is that instead of creating a just-in-case
default value every time, the defaultdict itself is constructed with a
factory method of some kind (in practice, it appears that this factory
method is usually the list or dict class constructor). If a reference
to the defaultdict gives a not-yet-existing key, then the factory
method is called to construct the new value, that value is stored in
the dict with the given key, and the value is passed back to the
caller. No instances are created unless they are truly required for
initializing an entry for a never-before-seen key.

-- Paul

Aug 17 '07 #6
On Aug 16, 6:03 pm, Carl Banks <pavlovevide...@gmail.comwrote:
On Aug 16, 6:35 pm, beginner <zyzhu2...@gmail.comwrote:
Hi All.
I'd like to do the following in more succint code:
if k in b:
a=b[k]
else:
a={}
b[k]=a
a['A']=1
In perl it is just one line: $a=$b->{"A"} ||={}.
Thanks,
Geoffrey

Define b as a default dict:

b = defaultdict(dict)
b[k]['A'] = l

Carl Banks- Hide quoted text -

- Show quoted text -
I think the most direct translation of the OP's example to Python
would be:

# setup b as special flavor of dict, that creates a
# new dict for not-yet-created keys
b = defaultdict(dict)

# actual python impl of $a=$b->{k}||={}
a = b[k]

# assign 1 to retrieved/created dict
a['A'] = 1

-- Paul

Aug 17 '07 #7
On Fri, 2007-08-17 at 00:53 +0000, beginner wrote:
$b is supposed to be a hash-table of hash-table. If a key exists in
$b, it points to another hash table. The $a=$b->{"A"} ||={} pattern is
useful when you want to add records to the double hash table.

For example, if you have a series of records in the format of (K1, K2,
V), and you want to add them to the double hash-table, you can do
$a=$b->{K1} || ={}
$a->{K2}=V
What is the best solution in Perl need not be the best solution in
Python. In Python you should just use a tuple as your dict key, i.e.
a[k1,k2] = v, unless you have some other constraints you're not telling
us.

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net
Aug 17 '07 #8
On Aug 16, 9:32 pm, Carsten Haese <cars...@uniqsys.comwrote:
On Fri, 2007-08-17 at 00:53 +0000, beginner wrote:
$b is supposed to be a hash-table of hash-table. If a key exists in
$b, it points to another hash table. The $a=$b->{"A"} ||={} pattern is
useful when you want to add records to the double hash table.
For example, if you have a series of records in the format of (K1, K2,
V), and you want to add them to the double hash-table, you can do
$a=$b->{K1} || ={}
$a->{K2}=V

What is the best solution in Perl need not be the best solution in
Python. In Python you should just use a tuple as your dict key, i.e.
a[k1,k2] = v, unless you have some other constraints you're not telling
us.

HTH,

--
Carsten Haesehttp://informixdb.sourceforge.net
I use tuples this way all the time. It is indeed very neat. But it is
not a replacement for double hash-table. If I want to retrieve
information just by K1, it is not efficient to index on (K1, K2).

Aug 17 '07 #9
On Aug 16, 5:35 pm, beginner <zyzhu2...@gmail.comwrote:
Hi All.

I'd like to do the following in more succint code:

if k in b:
a=b[k]
else:
a={}
b[k]=a

a['A']=1

In perl it is just one line: $a=$b->{"A"} ||={}.

Thanks,
Geoffrey
It looks like defaultdict is the solution for this kind of thing.
Thanks all for your help.

Aug 17 '07 #10
On Fri, 17 Aug 2007 03:15:10 -0000, beginner wrote
On Aug 16, 9:32 pm, Carsten Haese <cars...@uniqsys.comwrote:
What is the best solution in Perl need not be the best solution in
Python. In Python you should just use a tuple as your dict key, i.e.
a[k1,k2] = v, unless you have some other constraints you're not telling
us.

HTH,

--
Carsten Haesehttp://informixdb.sourceforge.net

I use tuples this way all the time. It is indeed very neat. But it
is not a replacement for double hash-table. If I want to retrieve
information just by K1, it is not efficient to index on (K1, K2).
If you have to look up all values associates with k1 and any k2, you're right,
that's not efficient. That would fall under "other constraints you're not
telling us." I'm not a mind reader.

-Carsten

Aug 17 '07 #11
beginner a écrit :
Hi All.

I'd like to do the following in more succint code:

if k in b:
a=b[k]
else:
a={}
b[k]=a

a['A']=1
In perl it is just one line: $a=$b->{"A"} ||={}.

Thanks,
Geoffrey

One solution I often use in such cases:

try:
a = b[k]
except KeyError: #or except IndexError: if b is a list/tuple and not a dict
a = {}
b[k] = a

a['A'] = 1

Indeed, exceptions are handled faster than "if/else" loops. As it was
mentionned earlier, One neat solution in Perl may not be the perfect one
in Python.

Cheers,

Sébastien
Aug 17 '07 #12
On Aug 17, 2:35 am, Sébastien Buchoux <seb.buch...@gmail.comwrote:
beginner a écrit :
Hi All.
I'd like to do the following in more succint code:
if k in b:
a=b[k]
else:
a={}
b[k]=a
a['A']=1
In perl it is just one line: $a=$b->{"A"} ||={}.
Thanks,
Geoffrey

One solution I often use in such cases:

try:
a = b[k]
except KeyError: #or except IndexError: if b is a list/tuple and not a dict
a = {}
b[k] = a

a['A'] = 1

Indeed, exceptions are handled faster than "if/else" loops. As it was
mentionned earlier, One neat solution in Perl may not be the perfect one
in Python.

Cheers,

Sébastien- Hide quoted text -

- Show quoted text -
Wow. This solution is interesting. I'll try this. Thanks.

Aug 17 '07 #13
On Aug 16, 11:02 pm, "Carsten Haese" <cars...@uniqsys.comwrote:
On Fri, 17 Aug 2007 03:15:10 -0000, beginner wrote
On Aug 16, 9:32 pm, Carsten Haese <cars...@uniqsys.comwrote:
What is the best solution in Perl need not be the best solution in
Python. In Python you should just use a tuple as your dict key, i.e.
a[k1,k2] = v, unless you have some other constraints you're not telling
us.
HTH,
--
Carsten Haesehttp://informixdb.sourceforge.net
I use tuples this way all the time. It is indeed very neat. But it
is not a replacement for double hash-table. If I want to retrieve
information just by K1, it is not efficient to index on (K1, K2).

If you have to look up all values associates with k1 and any k2, you're right,
that's not efficient. That would fall under "other constraints you're not
telling us." I'm not a mind reader.

-Carsten
Yeah, I should have mentioned that I actually want to group the data
by K1 and then by K2.

Aug 17 '07 #14
On Aug 16, 10:01 pm, Paul McGuire <pt...@austin.rr.comwrote:
On Aug 16, 8:28 pm, Jonathan Gardner

<jgardner.jonathangardner....@gmail.comwrote:
On Aug 16, 3:35 pm, beginner <zyzhu2...@gmail.comwrote:
In perl it is just one line: $a=$b->{"A"} ||={}.
a = b.setdefault('A', {})
This combines all two actions together:
- Sets b['A'] to {} if it is not already defined
- Assigns b['A'] to a
More info on dict methods here:
http://docs.python.org/lib/typesmapping.html

No, this has already been proposed and discarded. The OP does NOT
want this, because it always generates an empty {} whether it is
needed or not. Not really a big hardship, but if the default value
were some expensive-to-construct container class, then you would be
creating one every time you wanted to reference a value, on the chance
that the key did not exist.

Carl Banks' post using defaultdict is the correct solution. The
raison d'etre for defaultdict, and the reason that it is the solution
to the OP's question, is that instead of creating a just-in-case
default value every time, the defaultdict itself is constructed with a
factory method of some kind (in practice, it appears that this factory
method is usually the list or dict class constructor). If a reference
to the defaultdict gives a not-yet-existing key, then the factory
method is called to construct the new value, that value is stored in
the dict with the given key, and the value is passed back to the
caller. No instances are created unless they are truly required for
initializing an entry for a never-before-seen key.

When I made my response, it occurred to me that Python could be
improved (maybe) if one could overload dict.get() to use a factory,
like so:

b = {}
a = b.get(k,factory=dict)
a['A'] = 1

That's a slight improvement (maybe) over defaultdict since it would
still allow the same dict to have the membership check in other
places. I'm not so sure overloading get to let it modify the dict is
a good idea, though.

Actually, it'd probably be fairly uncontroversial to add a factory
keyword to dict.setdefault(). At least insofar as setdefault is
uncontroversial.
Carl Banks

Aug 18 '07 #15
Carl Banks wrote:
On Aug 16, 10:01 pm, Paul McGuire <pt...@austin.rr.comwrote:
>On Aug 16, 8:28 pm, Jonathan Gardner

<jgardner.jonathangardner....@gmail.comwrote:
>>On Aug 16, 3:35 pm, beginner <zyzhu2...@gmail.comwrote:
In perl it is just one line: $a=$b->{"A"} ||={}.
a = b.setdefault('A', {})
This combines all two actions together:
- Sets b['A'] to {} if it is not already defined
- Assigns b['A'] to a
More info on dict methods here:
http://docs.python.org/lib/typesmapping.html
No, this has already been proposed and discarded. The OP does NOT
want this, because it always generates an empty {} whether it is
needed or not. Not really a big hardship, but if the default value
were some expensive-to-construct container class, then you would be
creating one every time you wanted to reference a value, on the chance
that the key did not exist.

Carl Banks' post using defaultdict is the correct solution. The
raison d'etre for defaultdict, and the reason that it is the solution
to the OP's question, is that instead of creating a just-in-case
default value every time, the defaultdict itself is constructed with a
factory method of some kind (in practice, it appears that this factory
method is usually the list or dict class constructor). If a reference
to the defaultdict gives a not-yet-existing key, then the factory
method is called to construct the new value, that value is stored in
the dict with the given key, and the value is passed back to the
caller. No instances are created unless they are truly required for
initializing an entry for a never-before-seen key.


When I made my response, it occurred to me that Python could be
improved (maybe) if one could overload dict.get() to use a factory,
like so:

b = {}
a = b.get(k,factory=dict)
a['A'] = 1

That's a slight improvement (maybe) over defaultdict since it would
still allow the same dict to have the membership check in other
places. I'm not so sure overloading get to let it modify the dict is
a good idea, though.

Actually, it'd probably be fairly uncontroversial to add a factory
keyword to dict.setdefault(). At least insofar as setdefault is
uncontroversial.
Well it's uncontroversial enough to have made it into the distribution,
which represents a tacit admission by Guido that the .get() method alone
didn't implement his full vision.

The python-dev thread that discussed the feature before implementation
(as so often is the case) exercised many of the possible design paths,
and would be a useful read. [Damn, now I have to
well-known-search-engine it]. Aah, right - I'd forgotten how long it
took to get it right. This would be a suitable starting-point - it's the
beginning of the /third/ round of discussion:

http://mail.python.org/pipermail/pyt...ry/061485.html

Many alternatives were discussed, and my memory at this distance is that
Guido had good reasons for choosing the exact API he did for defaultdict.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Aug 18 '07 #16
beginner <zy*******@gmail.comwrote:
On Aug 16, 5:43 pm, ra...@dot.com (Lawrence Oluyede) wrote:
beginner <zyzhu2...@gmail.comwrote:
I'd like to do the following in more succint code:
if k in b:
a=b[k]
else:
a={}
b[k]=a
b.setdefault(k, a)

I am afraid it is not the same. b.setdefault(k, {}) will always create
an empty dict, even if k is in b
That is certainly true, but does it matter? You waste a very small
amount of time creating a dict you don't use.

$ python -m timeit '{}'
1000000 loops, best of 3: 0.247 usec per loop

On my machine 250 ns gets you a new dict...

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Aug 18 '07 #17
Paul McGuire <pt***@austin.rr.comwrote:
Carl Banks' post using defaultdict is the correct solution. The
raison d'etre for defaultdict, and the reason that it is the solution
to the OP's question, is that instead of creating a just-in-case
default value every time, the defaultdict itself is constructed with a
factory method of some kind (in practice, it appears that this factory
method is usually the list or dict class constructor). If a reference
to the defaultdict gives a not-yet-existing key, then the factory
method is called to construct the new value, that value is stored in
the dict with the given key, and the value is passed back to the
caller. No instances are created unless they are truly required for
initializing an entry for a never-before-seen key.
I think that if you truly want to emulate a perl hash then you would
want this which does the above but recursively.

from collections import defaultdict

class hash(defaultdict):
def __init__(self):
defaultdict.__init__(self, hash)

D=hash()

D[1][2][3][4]=5
D[1][4][5]=6

print D

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Aug 18 '07 #18
On Aug 18, 12:30 pm, Nick Craig-Wood <n...@craig-wood.comwrote:
I think that if you truly want to emulate a perl hash then you would
want this which does the above but recursively.

from collections import defaultdict

class hash(defaultdict):
def __init__(self):
defaultdict.__init__(self, hash)

D=hash()

D[1][2][3][4]=5
D[1][4][5]=6

print D

--
Nick Craig-Wood <n...@craig-wood.com--http://www.craig-wood.com/nick
Nick,
I thought I'd save your hash implementation away in my bag of tricks:
# File: autovivifying_dict.py
from collections import defaultdict
class hash(defaultdict):
""" Used like a dict except sub-dicts automagically created as
needed
Based on: http://groups.google.com/group/comp....34fbdafe4afa37
>>D=hash()
D[1][2][3][4]=5
D[1][4][5]=6
D
hash({1: hash({2: hash({3: hash({4: 5})}), 4: hash({5: 6})})})
>>hash({1: hash({2: hash({3: hash({4: 5})}), 4: hash({7: 8})})})
hash({1: hash({2: hash({3: hash({4: 5})}), 4: hash({7: 8})})})
>>>
"""
def __init__(self, *a, **b):
defaultdict.__init__(self, hash, *a, **b)
def __repr__(self):
return "hash(%s)" % (repr(dict(self)),)

def _test():
import doctest
doctest.testmod()

if __name__ == "__main__":
_test()

- Paddy.
Aug 18 '07 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Askari | last post by:
Yesterday, ALL code in python work and nothing when I close(finish) a code. Today, when I close, some raise this windows error : Instruction at "0x00FC3D70" use memory address "0x00000000". Can't...
40
by: Steve Juranich | last post by:
I know that this topic has the potential for blowing up in my face, but I can't help asking. I've been using Python since 1.5.1, so I'm not what you'd call a "n00b". I dutifully evangelize on the...
9
by: Lonnie Princehouse | last post by:
There doesn't seem to be any way to customize the behavior of "is" as can be done for other operators... why not?
28
by: john_sips_tea | last post by:
Just tried Ruby over the past two days. I won't bore you with the reasons I didn't like it, however one thing really struck me about it that I think we (the Python community) can learn from. ...
6
by: openopt | last post by:
Thank you in advance for your response. Dmitrey
25
by: samjnaa | last post by:
Please check for sanity and approve for posting at python-dev. In Visual Basic there is the keyword "with" which allows an object- name to be declared as governing the following statements. For...
92
by: ureuffyrtu955 | last post by:
Python is a good programming language, but "Python" is not a good name. First, python also means snake, Monty Python. If we search "python" in google, emule, many results are not programming...
6
by: grbgooglefan | last post by:
I am creating functions, the return result of which I am using to make decisions in combined expressions. In some expressions, I would like to inverse the return result of function. E.g....
10
by: Prisoner at War | last post by:
Hi, your friendly neighborhood n00b here, just wondering why on earth the Py3K folks want to mess with a simple thing like the "print" "command" (is that what it's called, a command?), turning it...
1
by: =?ISO-8859-1?Q?Andr=E9?= | last post by:
Hi everyone, I'd be interested in hearing suggestions as to the "best" way to drive a Python program step by step from another application. Details: --------- I have implemented a "Robot"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.