adding values to keys

Steve Holden steve at holdenweb.com
Fri Feb 15 11:00:02 EST 2008


Dennis Lee Bieber wrote:
> On Thu, 14 Feb 2008 23:55:21 -0800 (PST), Brandon
> <your.master at gmail.com> declaimed the following in comp.lang.python:
> 
>> Hi all,
>>
>> I'm not sure if I'm calling the right method in a dictionary.  I have:
>>
>> for k,v in dict.items():
> 
> 	Don't call your dictionary "dict" -- that overloads the builtin
> function...
> 
Allow me to pick a nit here: dict is a type, not a function (though as 
you clearly know, it's callable).

>>      NT = k,range(alpha,omega)        #where alpha and omega are
> 
> 	What are you doing with the "v"... If all you need is the key, then
> don't use the .items() method.
> 
>> previously defined as 1 and 4, respectively
>>      print NT
>>
>> which gives:
>> ('w', [0,1,2,3])
>> ('x', [0,1,2,3])
>> ('y', [0,1,2,3])
>> ('z', [0,1,2,3])
>>
>> And now I want a master dictionary like: [{'w': [0],[1],[2],[3]},

Do you want some variation on one of the following?

 >>> dct = {
...   'w': "something",
...   'z': "something else",
...   'x': "doesn't really matter",
...   'y': "because the values aren't used"
... }
 >>> mylst = [ [k, [[x] for x in range(4)]] for k in dct]
 >>> mylst
[['y', [[0], [1], [2], [3]]], ['x', [[0], [1], [2], [3]]], ['z', [[0], 
[1], [2], [3]]], ['w', [[0], [1], [2], [3]]]]
 >>> from pprint import pprint
 >>> pprint(tuple(mylst))
(['y', [[0], [1], [2], [3]]],
  ['x', [[0], [1], [2], [3]]],
  ['z', [[0], [1], [2], [3]]],
  ['w', [[0], [1], [2], [3]]])
 >>> pprint(dict(mylst))
{'w': [[0], [1], [2], [3]],
  'x': [[0], [1], [2], [3]],
  'y': [[0], [1], [2], [3]],
  'z': [[0], [1], [2], [3]]}
 >>>

> 	That is already impossible to achieve... you've specified four
> 1-element lists without packaging them into either a list or tuple of
> their own.
> 
>> {'x': [0]...]
>>
>> So I try:
>>
>> MT = {}
>> MT.fromkeys(NT[0], range(alpha,omega))
> 
> 	Note that NT is a single tuple -- your previous loop throws away the
> prior value and binds a new tuple each time. AND IT IS A TUPLE = ('z',
> [0, 1, 2, 3]), NT[0] is just "z" -- it does not have "keys" to use in
> the "fromkeys()" method.
> 
>> print MT
>>
>> but this only returns:
>> {}
>> {}
>> {}...
>>
>> Anybody see what I'm doing wrong?  Any advice is much appreciated.
>>
Well, one of the things you are doing wring is failing to specify your 
problem fully, but that's pretty normal for people overwhelmed by trying 
to come to terms with early programming tasks: I assume you'll learn 
better in time :-)
> 
> 	Show us code that can be executed -- even if it doesn't produce the
> results you expect -- as the snippets you gave can't be run as is...
> 
> 
>>>> adict = { "x" : "something",
> ... 	"y" : "else",
> ... 	"z" : "entirely"	}
>>>> bdict = adict.fromkeys(["y", "x"], range(3))
>>>> print bdict
> {'y': [0, 1, 2], 'x': [0, 1, 2]}
> 
> 	Note that you don't even need "adict" for that...
> 
>>>> bdict = {}.fromkeys(["y", "x"], range(3))
>>>> print bdict
> {'y': [0, 1, 2], 'x': [0, 1, 2]}
> 
>>>> cdict = {}.fromkeys(adict.keys(), "Lookie Here!!!")
>>>> print cdict
> {'y': 'Lookie Here!!!', 'x': 'Lookie Here!!!', 'z': 'Lookie Here!!!'}
> 
> 	Do any of the above give any enlightenment?
> 
trying-to-help-ly y'rs  - steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list