[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

adding values to keys

charonzen

2/15/2008 7:55:00 AM

Hi all,

I'm not sure if I'm calling the right method in a dictionary. I have:

for k,v in dict.items():
NT = k,range(alpha,omega) #where alpha and omega are
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]},
{'x': [0]...]

So I try:

MT = {}
MT.fromkeys(NT[0], range(alpha,omega))
print MT

but this only returns:
{}
{}
{}...

Anybody see what I'm doing wrong? Any advice is much appreciated.

Thanks,

Brandon
4 Answers

Bruno Desthuilliers

2/15/2008 8:35:00 AM

0

Brandon a écrit :
> 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 use 'dict' as an identifier, this shadows the builtin dict type.

> NT = k,range(alpha,omega) #where alpha and omega are
> previously defined as 1 and 4, respectively
> print NT

If you don't care about the values, you should iterate directly over the
keys - which is the default for dicts, ie:

for key in somedict:
print k

Also, by convention, ALL_CAPS names denote (pseudo) symbolic constants.

> which gives:
> ('w', [0,1,2,3])
> ('x', [0,1,2,3])
> ('y', [0,1,2,3])
> ('z', [0,1,2,3])

and by that time, NT == ('z', [0,1,2,3])

> And now I want a master dictionary like: [{'w': [0],[1],[2],[3]},
> {'x': [0]...]

This is a list of dicts, each one having a single key pointing to a
tuple of four one-element lists. Are you *sure* this is *really* what
you want ?

> So I try:
>
> MT = {}

this creates an empty dict instance...

> MT.fromkeys(NT[0], range(alpha,omega))

this calls the classmethod dict.fromkeys() on the empty dict instance
created by the previous statement, and discards the dict instance
created by the call to fromkeys().

Also, since at this stage NT is ('z', [0,1,2,3]), NT[0] is 'z', so the
dict created by fromkeys (and happily discarded) looked like:

{'z': [0, 1, 2, 3]}


> print MT
>
> but this only returns:
> {}

Indeed. You defined MT as an empty dict, didn't you ?

> {}
> {}...
>
> Anybody see what I'm doing wrong?

Quite a lot of things actually, but the worst one is probably failing to
read the FineManual(tm) !-)

Assuming that you have a dict d, and want to build another dict with d
keys and range(alpha,omega) for values, here's the solution:


alpha = 0
omega = 4

# arbitrary values, just for the exemple
d = dict(w=1, x=2, y=3, z=4)

master = dict.fromkeys(d, range(alpha, omega))

print master
=> {'y': [0, 1, 2, 3], 'x': [0, 1, 2, 3], 'z': [0, 1, 2, 3], 'w': [0, 1,
2, 3]}

Now note that this will associate each key of master with the *same*
list instance, so:

master['y'].append(42)
>>> print master
{'y': [0, 1, 2, 3, 42], 'x': [0, 1, 2, 3, 42], 'z': [0, 1, 2, 3, 42],
'w': [0, 1, 2, 3, 42]}

which is perhaps not what you want !-)

If you want distinct lists, dict.fromkeys is not the right method. You'd
better use the default constructor, passing it a sequence of key,value
tuples, ie:

master = dict((k, range(0,4)) for k in d)
print master
=> {'y': [0, 1, 2, 3], 'x': [0, 1, 2, 3], 'z': [0, 1, 2, 3], 'w': [0, 1,
2, 3]}
master['y'].append(42)
print master
{'y': [0, 1, 2, 3, 42], 'x': [0, 1, 2, 3], 'z': [0, 1, 2, 3], 'w': [0,
1, 2, 3]}


> Any advice is much appreciated.

Ok:
- read the FineManual(tm)
- learn to use the interactive Python shell
- read the FineManual(tm)
- learn to use the help feature of the interactive Python shell
- read the FineManual(tm)
- read pep08 on naming conventions
- read the FineManual(tm)

!-)

HTH

Dennis Lee Bieber

2/15/2008 8:36:00 AM

0

On Thu, 14 Feb 2008 23:55:21 -0800 (PST), Brandon
<your.master@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...

> 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]},

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.
>

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?

--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/

Bruno Desthuilliers

2/15/2008 8:45:00 AM

0

Dennis Lee Bieber a écrit :
> On Thu, 14 Feb 2008 23:55:21 -0800 (PST), Brandon
> <your.master@gmail.com> declaimed the following in comp.lang.python:
(snip)
>> 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.

Note that the first arg to dict.fromkeys doesn't need to have a .keys
method - you can pass in any iterable, and even a single hashable (in
which case your ditc will only have one key, of course).

>>> dict.fromkeys('abc')
{'a': None, 'c': None, 'b': None}
>>> dict.fromkeys(c for c in 'abc')
{'a': None, 'c': None, 'b': None}
>>> dict.fromkeys((1,2,3))
{1: None, 2: None, 3: None}
>>> dict.fromkeys('a')
{'a': None}


Steve Holden

2/15/2008 4:00:00 PM

0

Dennis Lee Bieber wrote:
> On Thu, 14 Feb 2008 23:55:21 -0800 (PST), Brandon
> <your.master@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.hold...