更新python字典 [英] Updating python dictionary

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

问题描述

您好...


我有一个键/值的字典,我想更改其中的键,基于

在另一个映射字典中。举例如下:


MAPPING_DICT = {

''a'':''A'',

''b '':''B'',

}


my_dict = {

''''':''1 '',

''b'':''2''

}


我希望完成的my_dict看起来喜欢:


my_dict = {

''A'':''1'',

''B'' :''2''

}


因此原来的my_dict中的键被换掉了

键映射在MAPPING_DICT。


是否有一种聪明的方法可以做到这一点,或者我应该通过这两种方式,

基本上创建一个全新的字典?


干杯,

Andy。

Hello...

I have a dict of key/values and I want to change the keys in it, based
on another mapping dictionary. An example follows:

MAPPING_DICT = {
''a'': ''A'',
''b'': ''B'',
}

my_dict = {
''a'': ''1'',
''b'': ''2''
}

I want the finished my_dict to look like:

my_dict = {
''A'': ''1'',
''B'': ''2''
}

Whereby the keys in the original my_dict have been swapped out for the
keys mapped in MAPPING_DICT.

Is there a clever way to do this, or should I loop through both,
essentially creating a brand new dict?

Cheers,
Andy.

推荐答案

On Sun,2008年9月7日14:51 :32 -0700, an******@gmail.com 写道:
On Sun, 07 Sep 2008 14:51:32 -0700, an******@gmail.com wrote:

MAPPING_DICT = {

''a'':''A'',

''b'':''B'',

}


my_dict = {

''a'':''1'',

''b'':''2''

}


我希望完成的my_dict看起来像:


my_dict = {

'' A'':''1'',

''B'':''2''

}


因此,原始my_dict中的键已被换出映射到MAPPING_DICT中的

键。


是否有一种聪明的方法可以做到这一点,或者我应该循环通过两者,

基本上创造了一个全新的字典?
MAPPING_DICT = {
''a'': ''A'',
''b'': ''B'',
}

my_dict = {
''a'': ''1'',
''b'': ''2''
}

I want the finished my_dict to look like:

my_dict = {
''A'': ''1'',
''B'': ''2''
}

Whereby the keys in the original my_dict have been swapped out for the
keys mapped in MAPPING_DICT.

Is there a clever way to do this, or should I loop through both,
essentially creating a brand new dict?



你只需循环通过`my_dict`:


在[187]:%cpaste

粘贴代码;单独输入'' - ''就行了。

:MAPPING_DICT = {

:'''':''A'',
:''b'':''B'',

:}



:my_dict = {

:'''':''1'',

:''b'':''2''

:}

: -


在[188]中:dict((MAPPING_DICT [k],v)代表k,v代表my_dict.iteritems())

Out [188]:{''A'':''1'','B'':''2''}


Ciao ,

Marc''BlackJack''Rintsch

You only have to loop through `my_dict`:

In [187]: %cpaste
Pasting code; enter ''--'' alone on the line to stop.
:MAPPING_DICT = {
: ''a'': ''A'',
: ''b'': ''B'',
:}
:
:my_dict = {
: ''a'': ''1'',
: ''b'': ''2''
:}
:--

In [188]: dict((MAPPING_DICT[k], v) for k, v in my_dict.iteritems())
Out[188]: {''A'': ''1'', ''B'': ''2''}

Ciao,
Marc ''BlackJack'' Rintsch


9月7日,2:51 * pm,andyh ... @ gmail。 COM" < andyh ... @ gmail.comwrote:
On Sep 7, 2:51*pm, "andyh...@gmail.com" <andyh...@gmail.comwrote:

你好......


我有一个密钥/值和我想要更改其中的键,基于另一个映射字典上的
。举例如下:


MAPPING_DICT = {

* *'''':''A'',

* *''''':''B'',


}


my_dict = {

* *''''':''1'',

* *''b'':''2''


}


我希望完成的my_dict看起来像:


my_dict = {

* *''A'':' '1'',

* *''B'':''2''


}


因此原始my_dict中的键已被换出来映射到MAPPING_DICT中的

键。


是否有一种聪明的方法可以做到这一点,或者应该我循环了两个,

基本上创造了一个全新的字典?


干杯,

安迪。
Hello...

I have a dict of key/values and I want to change the keys in it, based
on another mapping dictionary. An example follows:

MAPPING_DICT = {
* * ''a'': ''A'',
* * ''b'': ''B'',

}

my_dict = {
* * ''a'': ''1'',
* * ''b'': ''2''

}

I want the finished my_dict to look like:

my_dict = {
* * ''A'': ''1'',
* * ''B'': ''2''

}

Whereby the keys in the original my_dict have been swapped out for the
keys mapped in MAPPING_DICT.

Is there a clever way to do this, or should I loop through both,
essentially creating a brand new dict?

Cheers,
Andy.



您可以通过类似以下内容完成此操作:

new_dict = {}

for x in MAPPING_DICT.keys():

如果my在my_dict.keys()中:

new_dict [MAPPING_DICT [x]] = my_dict [x]

虽然可能有更好的解决方案,你的

。也许更多的细节可以帮助我们引导你正确的方向

方向?

干杯,MK

You could accomplish this with something similar to the following:
new_dict = {}
for x in MAPPING_DICT.keys():
if x in my_dict.keys():
new_dict[MAPPING_DICT[x]] = my_dict[x]

Although there is probably a better solution to the problem your
having. Perhaps more details could help us lead in you the right
direction?

Cheers, MK


an******@gmail.com 写道:
an******@gmail.com wrote:

有没有一种聪明的方法可以做到这一点,或者我应该循环使用两者,

基本上创建一个全新的字典?
Is there a clever way to do this, or should I loop through both,
essentially creating a brand new dict?



因为你的词典中没有一个包含你想要的最终

字典中的键,所以创建一个全新的字典对我来说听起来很聪明。


< / F>

since none of your dictionaries contain the keys you want in the final
dictionary, creating a brand new dict sounds pretty clever to me.

</F>


这篇关于更新python字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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