Re: dictionary of dictionaries

2007-12-11 Thread Kay Schluehr
On Dec 9, 9:35 am, kettle <[EMAIL PROTECTED]> wrote: > Hi, > I'm wondering what the best practice is for creating an extensible > dictionary-of-dictionaries in python? > > In perl I would just do something like: > > my %hash_of_hashes; > for(my $i=0;$i<10;$i++){ > for(my $j=0;$j<10;$j++){ >

Re: dictionary of dictionaries

2007-12-11 Thread Marc 'BlackJack' Rintsch
On Mon, 10 Dec 2007 23:51:00 -0800, kettle wrote: > On Dec 10, 6:58 pm, Peter Otten <[EMAIL PROTECTED]> wrote: >> Well, there's also dict.setdefault() >> >> >>> pairs = ["ab", "ab", "ac", "bc"] >> >>> outer = {} >> >>> for a, b in pairs: >> >> ... inner = outer.setdefault(a, {}) >> ... inn

Re: dictionary of dictionaries

2007-12-10 Thread kettle
On Dec 10, 6:58 pm, Peter Otten <[EMAIL PROTECTED]> wrote: > kettle wrote: > > On Dec 9, 5:49 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > >> On Sun, 09 Dec 2007 00:35:18 -0800, kettle wrote: > >> > Hi, > >> > I'm wondering what the best practice is for creating an extensible > >> > d

Re: dictionary of dictionaries

2007-12-10 Thread kettle
On Dec 10, 6:58 pm, Peter Otten <[EMAIL PROTECTED]> wrote: > kettle wrote: > > On Dec 9, 5:49 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > >> On Sun, 09 Dec 2007 00:35:18 -0800, kettle wrote: > >> > Hi, > >> > I'm wondering what the best practice is for creating an extensible > >> > d

Re: dictionary of dictionaries

2007-12-10 Thread Peter Otten
kettle wrote: > On Dec 9, 5:49 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: >> On Sun, 09 Dec 2007 00:35:18 -0800, kettle wrote: >> > Hi, >> > I'm wondering what the best practice is for creating an extensible >> > dictionary-of-dictionaries in python? >> >> > In perl I would just do

Re: dictionary of dictionaries

2007-12-09 Thread kettle
On Dec 9, 5:49 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > On Sun, 09 Dec 2007 00:35:18 -0800, kettle wrote: > > Hi, > > I'm wondering what the best practice is for creating an extensible > > dictionary-of-dictionaries in python? > > > In perl I would just do something like: > > > m

Re: dictionary of dictionaries

2007-12-09 Thread Marc 'BlackJack' Rintsch
On Sun, 09 Dec 2007 00:35:18 -0800, kettle wrote: > Hi, > I'm wondering what the best practice is for creating an extensible > dictionary-of-dictionaries in python? > > In perl I would just do something like: > > my %hash_of_hashes; > for(my $i=0;$i<10;$i++){ > for(my $j=0;$j<10;$j++){ >

Re: Dictionary of Dictionaries

2007-03-05 Thread Duncan Booth
"Bart Ogryczak" <[EMAIL PROTECTED]> wrote: > On Mar 5, 11:22 am, [EMAIL PROTECTED] wrote: >> messagesReceived = dict.fromkeys(("one","two"), {}) > > This creates two references to just *one* instance of empty > dictionary. > I'd do it like: > messagesReceived = dict([(key, {}) for key in ("one","

Re: Dictionary of Dictionaries

2007-03-05 Thread Bart Ogryczak
On Mar 5, 11:22 am, [EMAIL PROTECTED] wrote: > messagesReceived = dict.fromkeys(("one","two"), {}) This creates two references to just *one* instance of empty dictionary. I'd do it like: messagesReceived = dict([(key, {}) for key in ("one","two")]) -- http://mail.python.org/mailman/listinfo/p

Re: Dictionary of Dictionaries

2007-03-05 Thread bg_ie
On 5 Mar, 11:45, "Amit Khemka" <[EMAIL PROTECTED]> wrote: > On 5 Mar 2007 02:22:24 -0800, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > > Hi, > > > I have the following - > > > messagesReceived = dict.fromkeys(("one","two"), {}) > > This will create a dictionary "messagesReceived", with all the

Re: Dictionary of Dictionaries

2007-03-05 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, bg_ie wrote: > What am I doing wrong? `dict.fromkeys()` stores the given object for all keys, so you end up with the *same* dictionary for 'one' and 'two'. In [18]: a = dict.fromkeys(("one","two"), {}) In [19]: a Out[19]: {'two': {}, 'one': {}} In [20]: a['one']['x'] =

Re: Dictionary of Dictionaries

2007-03-05 Thread Amit Khemka
On 5 Mar 2007 02:22:24 -0800, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Hi, > > I have the following - > > messagesReceived = dict.fromkeys(("one","two"), {}) This will create a dictionary "messagesReceived", with all the keys referring to *same instance* of the (empty) dictionary. ( try: m