Re: [Tutor] how do I create a lists of values associated with a key?
Message: 8 Date: Thu, 31 Jul 2008 20:16:54 -0700 (PDT) From: Angela Yang [EMAIL PROTECTED] Subject: [Tutor] how do I create a lists of values associated with a key? To: tutor@python.org Message-ID: [EMAIL PROTECTED] Content-Type: text/plain; charset=iso-8859-1 That did not work because list index is not numeric. Dictionary is an unordered set (list is an ordered set) indexed by a immutable value (e.g. string, numbers, tuple) But for dictionaries, it is key - value pairs.? But I need key - multiple values. That would mean, a dictionary of lists. Dictionary keys cannot be a mutable (i.e. list cannot be a dictionary key) but dictionary's value may be mutable. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] how do I create a lists of values associated with a key?
Hi, I have a list of values for one key. How do I specify this data structure? First tried, collection = [] collection['abby'].append('apprentice1') collection['abby'].append('apprentice2') That did not work because list index is not numeric. But for dictionaries, it is key - value pairs. But I need key - multiple values. Do you have some example how to specify this? Angela ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how do I create a lists of values associated with a key?
On Thu, Jul 31, 2008 at 8:16 PM, Angela Yang [EMAIL PROTECTED] wrote: Hi, I have a list of values for one key. How do I specify this data structure? First tried, collection = [] collection['abby'].append('apprentice1') collection['abby'].append('apprentice2') That did not work because list index is not numeric. But for dictionaries, it is key - value pairs. But I need key - multiple values. Do you have some example how to specify this? Angela Each value can, itself, be a container - a tuple, a list, or another dictionary. dictTuple = {a:(1,2,3), b:(4,5,6)} dictList = {a:[1,2,3], b:[4,5,6]} dictDict = {a:{c:1,d:2,e:3}, b:{f:4,g:5,h:6}} Retrieving values: valValue = dictTuple[a][0] # 1 valValue = dictTuple[b][2] # 6 Lists work just the same: valValue = dictList[a][0] # 1 valValue = dictList[b][2] # 6 Dictionaries are, well, like dictionaries: valValue = dictDict[a][c] # 1 valValue = dictDict[b][h] # 6 Hope that helps -- www.fsrtechnologies.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how do I create a lists of values associated with a key?
Angela Yang [EMAIL PROTECTED] wrote But for dictionaries, it is key - value pairs. But I need key - multiple values. But a value can be a list. d = {} d['odd'] = [1,3,5,7] d['even'] = [2,4,6,8] print d['odd'][2]# = 5 See the Raw Materials top[ic in my tutorial for another example using a dictionary to store address data: addressBook = { ... 'Fred' : ['Fred', '9 Some St',' Anytown', '0123456789'], ... 'Rose' : ['Rose', '11 Nother St', 'SomePlace', '0987654321'] ... } print addressBook['Rose'] ['Rose', '11 Nother St', 'SomePlace', '0987654321'] print addressBook['Fred'][3] 0123456789 HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how do I create a lists of values associated with a key?
Dear Angela, in order to do this, the setdefault function of the dictionaries is very useful. For example: mydict = {} mylist = mydict.setdefault(mykey, []) mylist.append(myvalue) setdefault either returns the already existing list or sets a new list for the key and returns it. Regards, Benoit Thiell. On Fri, 1 Aug 2008, Marc Tompkins wrote: On Thu, Jul 31, 2008 at 8:16 PM, Angela Yang [EMAIL PROTECTED] wrote: Hi, I have a list of values for one key. How do I specify this data structure? First tried, collection = [] collection['abby'].append('apprentice1') collection['abby'].append('apprentice2') That did not work because list index is not numeric. But for dictionaries, it is key - value pairs. But I need key - multiple values. Do you have some example how to specify this? Angela Each value can, itself, be a container - a tuple, a list, or another dictionary. dictTuple = {a:(1,2,3), b:(4,5,6)} dictList = {a:[1,2,3], b:[4,5,6]} dictDict = {a:{c:1,d:2,e:3}, b:{f:4,g:5,h:6}} Retrieving values: valValue = dictTuple[a][0] # 1 valValue = dictTuple[b][2] # 6 Lists work just the same: valValue = dictList[a][0] # 1 valValue = dictList[b][2] # 6 Dictionaries are, well, like dictionaries: valValue = dictDict[a][c] # 1 valValue = dictDict[b][h] # 6 Hope that helps -- www.fsrtechnologies.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how do I create a lists of values associated with a key?
On Thu, Jul 31, 2008 at 11:16 PM, Angela Yang [EMAIL PROTECTED] wrote: Hi, I have a list of values for one key. How do I specify this data structure? First tried, collection = [] collection['abby'].append('apprentice1') collection['abby'].append('apprentice2') That did not work because list index is not numeric. But for dictionaries, it is key - value pairs. But I need key - multiple values. A dict or defaultdict with list values would work well here. The defaultdict has the advantage of not requiring any user code to handle missing keys: In [7]: from collections import defaultdict In [8]: c=defaultdict(list) In [9]: c['abby'].append('apprentice1') In [10]: c['abby'].append('apprentice2') In [11]: c Out[11]: defaultdict(type 'list', {'abby': ['apprentice1', 'apprentice2']}) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how do I create a lists of values associated with a key?
2008/8/1 Angela Yang [EMAIL PROTECTED] collection = [] collection['abby'].append('apprentice1') collection['abby'].append('apprentice2') That did not work because list index is not numeric. But for dictionaries, it is key - value pairs. But I need key - multiple values. Do you have some example how to specify this? You already know this : Collection1 = [] Collection2 = [] Collection1.append('apprentice1') Collection1.append('apprentice2') if you add a MainCollection to the picture which can be coded like this: MainCollection = {'Collection1':[], 'Collection2':[]} Adding elements to a Collection is as simple as this interactive session demonstrates : Python 2.4.4 (#2, Apr 15 2008, 23:43:20) [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2 Type help, copyright, credits or license for more information. MainCollection = {'Collection1':[], 'Collection2':[]} type(MainCollection) type 'dict' MainCollection['Collection1'] [] MainCollection['Collection1'].append('aa') MainCollection['Collection1'] ['aa'] MainCollection['Collection1'].append('bb') MainCollection['Collection1'] ['aa', 'bb'] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor