[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.python

Creating variables from dicts

vsoler

2/23/2010 8:53:00 PM

Hi,

I have two dicts

n={'a', 'm', 'p'}
v={1,3,7}

and I'd like to have

a=1
m=3
p=7

that is, creating some variables.

How can I do this?
22 Answers

Arnaud Delobelle

2/23/2010 9:10:00 PM

0

vsoler <vicente.soler@gmail.com> writes:

> Hi,
>
> I have two dicts
>
> n={'a', 'm', 'p'}
> v={1,3,7}

These are sets, not dicts.

> and I'd like to have
>
> a=1
> m=3
> p=7

As sets are unordered, you may as well have

a = 3
m = 7
p = 1

or any other permutation. You need some sequences instead. E.g.

n = ['a', 'm', 'p']
v = (1, 3, 7)

Then you can do:

for name, value in zip(n, v):
globals()[name] = value

After this the names, 'a', 'm' and 'p' will be bound to the values you
want in the global namespace. However, it is almost always a bad idea
to do this. Can you explain why you need to do this?

--
Arnaud

Hai Vu

2/23/2010 9:22:00 PM

0

On Feb 23, 12:53 pm, vsoler <vicente.so...@gmail.com> wrote:
> Hi,
>
> I have two dicts
>
> n={'a', 'm', 'p'}
> v={1,3,7}
>
> and I'd like to have
>
> a=1
> m=3
> p=7
>
> that is, creating some variables.
>
> How can I do this?

I think you meant to use the square brackets [ ] instead of the curly
ones { } to define the list:

>>> n = ['a', 'b', 'c']
>>> v = [3, 5, 7]
>>> for x, y in zip(n, v):
.... exec '%s=%d' % (x, y)
....
>>> a
3
>>> b
5
>>> c
7

---
The key is the use of the exec statement, which executes the strings
"a=3", "b=5", ... as if they are python statements.

MRAB

2/23/2010 9:32:00 PM

0

vsoler wrote:
> Hi,
>
> I have two dicts
>
> n={'a', 'm', 'p'}
> v={1,3,7}
>
Those aren't dicts, they're sets.

> and I'd like to have
>
> a=1
> m=3
> p=7
>
> that is, creating some variables.
>
> How can I do this?

The real question is not how, but why?

Anyway, assuming you want them to be global variables:

globals().update(dict(zip(n, v)))

On my machine the variables didn't get the correct values because, as I
said, 'n' and 'v' are sets, so the order of the members is arbitrary.

Luis M. González

2/23/2010 10:57:00 PM

0

On Feb 23, 5:53 pm, vsoler <vicente.so...@gmail.com> wrote:
> Hi,
>
> I have two dicts
>
> n={'a', 'm', 'p'}
> v={1,3,7}
>
> and I'd like to have
>
> a=1
> m=3
> p=7
>
> that is, creating some variables.
>
> How can I do this?

You are probably coming from another language and you're not used to
python's data structures.
If you want a list of items, you use tuples or lists. Examples:

('a', 'm', 'p') ---> this is a tuple, and it's made with
parenthesis ()
['a', 'm', 'p'] ---> this is a list, and it's made with brackets
[]

Check the documentation to see the difference between tuples and
lists.
For now, lets just use lists and forget about tuples...
Now if you want a sequence of items ordered a key + value pairs, use a
dictionary, as follows:

{'name': 'joe', 'surname': 'doe', 'age': 21} ---> this is a dict,
and it's made with curly braces {}.

Curly braces are also used to create sets, but you don't need them now
(check the documentation to learn more about sets).
So going back to your question, you should have two lists, as follows:

n = ['a', 'm', 'p']
v = [1,3,7] --> note that I used brackets [], not curly
braces {}.

And now you can build a dict formed by the keys in "n" and the values
in "v":

myDict = {} --> this is an new empty dictionary
for k,v in zip(n,v):
myDict[k] = v

This results in this dictionary: {'a': 1, 'p': 7, 'm': 3}.

Hope this helps...
Luis

Luis M. González

2/23/2010 11:41:00 PM

0

On Feb 23, 7:56 pm, Luis M. González <luis...@gmail.com> wrote:
> On Feb 23, 5:53 pm, vsoler <vicente.so...@gmail.com> wrote:
>
>
>
>
>
> > Hi,
>
> > I have two dicts
>
> > n={'a', 'm', 'p'}
> > v={1,3,7}
>
> > and I'd like to have
>
> > a=1
> > m=3
> > p=7
>
> > that is, creating some variables.
>
> > How can I do this?
>
> You are probably coming from another language and you're not used to
> python's data structures.
> If you want a list of items, you use tuples or lists. Examples:
>
>     ('a', 'm', 'p') ---> this is a tuple, and it's made with
> parenthesis ()
>     ['a', 'm', 'p'] ---> this is a list, and it's made with brackets
> []
>
> Check the documentation to see the difference between tuples and
> lists.
> For now, lets just use lists and forget about tuples...
> Now if you want a sequence of items ordered a key + value pairs, use a
> dictionary, as follows:
>
>     {'name': 'joe', 'surname': 'doe', 'age': 21} ---> this is a dict,
> and it's made with curly braces {}.
>
> Curly braces are also used to create sets, but you don't need them now
> (check the documentation to learn more about sets).
> So going back to your question, you should have two lists, as follows:
>
>     n = ['a', 'm', 'p']
>     v = [1,3,7]         --> note that I used brackets [], not curly
> braces {}.
>
> And now you can build a dict formed by the keys in "n" and the values
> in "v":
>
>     myDict = {}           --> this is an new empty dictionary
>     for k,v in zip(n,v):
>         myDict[k] = v
>
> This results in this dictionary: {'a': 1, 'p': 7, 'm': 3}.
>
> Hope this helps...
> Luis

By the way, if you want the variables inside myDict to be free
variables, you have to add them to the local namespace.
The local namespace is also a dictionary "locals()".
So you can update locals as follows:

locals().update( myDictionary )

This way, all the key-value pairs inside myDict become free variables.
Wich is the same as declaring them as follows:

a = 1
p = 7

etc...

Luis



Tim Chase

2/23/2010 11:42:00 PM

0

Luis M. González wrote:
> If you want a list of items, you use tuples or lists. Examples:
>
> ('a', 'm', 'p') ---> this is a tuple, and it's made with
> parenthesis ()

Actually, a tuple is made with commas...the parens are just there
to clarify the order of operations and make it easier to read :)

>>> x = 1,2,3
>>> x
(1, 2, 3)

-tkc





Steven D'Aprano

2/24/2010 1:41:00 AM

0

On Tue, 23 Feb 2010 15:41:16 -0800, Luis M. González wrote:

> By the way, if you want the variables inside myDict to be free
> variables, you have to add them to the local namespace. The local
> namespace is also a dictionary "locals()". So you can update locals as
> follows:
>
> locals().update( myDictionary )

No you can't. Try it inside a function.




--
Steven

Luis M. González

2/24/2010 3:47:00 AM

0

On Feb 23, 10:41 pm, Steven D'Aprano
<ste...@REMOVE.THIS.cybersource.com.au> wrote:
> On Tue, 23 Feb 2010 15:41:16 -0800, Luis M. González wrote:
> > By the way, if you want the variables inside myDict to be free
> > variables, you have to add them to the local namespace. The local
> > namespace is also a dictionary "locals()". So you can update locals as
> > follows:
>
> >     locals().update( myDictionary )
>
> No you can't. Try it inside a function.
>
> --
> Steven

Sure. Inside a function I would use globals() instead.
Although I don't know if it would be a good practice...

Luis

Steven D'Aprano

2/24/2010 4:16:00 AM

0

On Tue, 23 Feb 2010 19:47:22 -0800, Luis M. González wrote:

> On Feb 23, 10:41 pm, Steven D'Aprano
> <ste...@REMOVE.THIS.cybersource.com.au> wrote:
>> On Tue, 23 Feb 2010 15:41:16 -0800, Luis M. González wrote:
>> > By the way, if you want the variables inside myDict to be free
>> > variables, you have to add them to the local namespace. The local
>> > namespace is also a dictionary "locals()". So you can update locals
>> > as follows:
>>
>> >     locals().update( myDictionary )
>>
>> No you can't. Try it inside a function.
>>
>> --
>> Steven
>
> Sure. Inside a function I would use globals() instead. Although I don't
> know if it would be a good practice...

Er, how does using globals change the local variables?



--
Steven

Luis M. González

2/24/2010 4:44:00 AM

0

On Feb 24, 1:15 am, Steven D'Aprano
<ste...@REMOVE.THIS.cybersource.com.au> wrote:
> On Tue, 23 Feb 2010 19:47:22 -0800, Luis M. González wrote:
> > On Feb 23, 10:41 pm, Steven D'Aprano
> > <ste...@REMOVE.THIS.cybersource.com.au> wrote:
> >> On Tue, 23 Feb 2010 15:41:16 -0800, Luis M. González wrote:
> >> > By the way, if you want the variables inside myDict to be free
> >> > variables, you have to add them to the local namespace. The local
> >> > namespace is also a dictionary "locals()". So you can update locals
> >> > as follows:
>
> >> >     locals().update( myDictionary )
>
> >> No you can't. Try it inside a function.
>
> >> --
> >> Steven
>
> > Sure. Inside a function I would use globals() instead. Although I don't
> > know if it would be a good practice...
>
> Er, how does using globals change the local variables?
>
> --
> Steven

Hmmm.... well, you tell me!
As I said, I don't know if this is the recomended way...

Luis