"提取"一本字典 [英] "Extracting" a dictionary

查看:74
本文介绍了"提取"一本字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我对Python很陌生,而且因为对

文档的不那么肤浅的看法没有回答我的问题(虽然它仍然感觉很基本),我

决定转向这个地方:


有没有办法''提取''当前命名空间的字典?

也就是说,如果你有

{''foo'':23,''bar'':42}

你会得到一个值为23的变量foo和一个值为

42的变量条吗?这样的功能当然只适用于字符串键,并且可能需要检查它,但是,它仍然听起来很实用,

肯定是其他人想到的。


Daniel

Hello,

I''m quite new to Python, and since a not-so-superficial look into the
docs didn''t answer my question (although it still feels quite basic), I
decided to turn to this place:

Is there a way to ''extract'' a dictionary into the current namespace?
That is, if you have
{''foo'' : 23, ''bar'' : 42}
you would get a variable foo with value 23 and a variable bar with value
42? Such a function would of course only work on string keys and would
probably have to check that, but still, it sounds practical enough that
surely someone else thought of it before.

Daniel

推荐答案

Daniel Klein写道:
Daniel Klein wrote:
有没有办法将字典''提取'到当前的命名空间?
也就是说,如果你有
{''foo'':23,''bar'':42} <你会得到一个值为23的变量foo和一个值为
42的变量条吗?这样的功能当然只适用于字符串键,并且可能需要检查它,但是,它听起来还不够实际,以前肯定有人想过它。
Is there a way to ''extract'' a dictionary into the current namespace?
That is, if you have
{''foo'' : 23, ''bar'' : 42}
you would get a variable foo with value 23 and a variable bar with value
42? Such a function would of course only work on string keys and would
probably have to check that, but still, it sounds practical enough that
surely someone else thought of it before.

vars = {''foo'':23,''bar'':42}
当地人( ).update(vars)
foo
23 bar
vars = {''foo'': 23, ''bar'': 42}
locals().update(vars)
foo 23 bar





42


Leif K-Brooks写道:
Leif K-Brooks wrote:
Daniel Klein写道:
Daniel Klein wrote:
有没有办法将字典''提取'成当前的命名空间?
也就是说,如果你有
{''foo'':23,''bar'':42}
你会得到一个变量foo,其值为23和a值为42的变量条?这样的功能当然只适用于字符串键,
可能需要检查一下,但它仍然听起来很实用,以前其他人肯定会想到它。
Is there a way to ''extract'' a dictionary into the current namespace?
That is, if you have
{''foo'' : 23, ''bar'' : 42}
you would get a variable foo with value 23 and a variable bar with
value 42? Such a function would of course only work on string keys and
would probably have to check that, but still, it sounds practical
enough that surely someone else thought of it before.




>>> vars = {''foo'':23,''bar'':42}
>>> locals()。update(vars)
>>> foo 23>>> bar
>>> vars = {''foo'': 23, ''bar'': 42}
>>> locals().update(vars)
>>> foo 23 >>> bar


42


42




除了本页的说明
http://docs.python.org/lib/built-in -...内置功能

""

locals()


更新并返回代表当前本地符号的字典

表。警告:此词典的内容不应该是修改的b $ b;更改可能不会影响

翻译所使用的局部变量值。

"""


所以Don不要这样做。


-Peter



Except note from this page
http://docs.python.org/lib/built-in-...built-in-funcs that
"""
locals()

Update and return a dictionary representing the current local symbol
table. Warning: The contents of this dictionary should not be
modified; changes may not affect the values of local variables used by
the interpreter.
"""

So Don''t Do That.

-Peter


Am Montag,17。Mai 2004 21:34 schrieb Leif K-Brooks :
Am Montag, 17. Mai 2004 21:34 schrieb Leif K-Brooks:
>>> locals()。update(vars)
>>> locals().update(vars)




来自文档:


" "

locals()

更新并返回表示当前本地符号表的字典。

警告:不应修改此词典的内容;更改可能

不会影响口译员使用的局部变量的值。

"""


Heiko。



From the documentation:

"""
locals()

Update and return a dictionary representing the current local symbol table.
Warning: The contents of this dictionary should not be modified; changes may
not affect the values of local variables used by the interpreter.
"""

Heiko.


这篇关于&QUOT;提取&QUOT;一本字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆