Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 复制多级Python字典_Python 2.7_Dictionary_Copy - Fatal编程技术网

Python 2.7 复制多级Python字典

Python 2.7 复制多级Python字典,python-2.7,dictionary,copy,Python 2.7,Dictionary,Copy,我有下面的Python字典作为source d1 = { 'a': 1, 'b': 2, 'c': [{'d': 3, 'e': 4, 'un': 'wanted1', 'dont': 'needthis1'}, {'d': 5, 'e': 6, 'un': 'wanted2', 'dont': 'needthis2'}] 'xyz': 'abc', 'zxy': 'cab', 'wva': 'xyw' } 我想把一些特定键的值复制到另一个dict中,形

我有下面的Python字典作为
source

d1 = {
  'a': 1,
  'b': 2,
  'c': [{'d': 3, 'e': 4, 'un': 'wanted1', 'dont': 'needthis1'},
        {'d': 5, 'e': 6, 'un': 'wanted2', 'dont': 'needthis2'}]
  'xyz': 'abc',
  'zxy': 'cab',
  'wva': 'xyw'
}
我想把一些特定键的值复制到另一个dict中,形成下面的
target
dictionary

d2 = {
  'some_attr_1': 1,  
  'some_attr_x': 2,
  'attr_some_z': [{'attr_x': 3, 'attrib': 4},
                  {'attr_x': 5, 'attrib': 6}]
}
注:

  • 我对
    source
    例如:我不需要钥匙
    xyz
    zxy
    ,等等
  • 基本上,希望映射
    中某些键的值 到
    目标中的不同键
    
我目前的方法是在
目标
字典键之间进行映射

attr_map1 = {
   'some_attr_1': 'a',
   'some_attr_x': 'b'
}

attr_map2 = {
  'attr_x': 'd',
  'attrib': 'e',
}

d2 = dict()
for k, v in attr_map1.items():
   d2[k] = d1[v]

l1 = list()
for d_elem in d1['c']:
   temp_dict = dict()
   for k, v in attr_map2.items():
       temp_dict[k] = d_elem[v]
   l1.append(temp_dict)
d2['attr_some_z'] = l1
是否有其他更好、更快的方法来实现这一目标


我正在寻找Python 2.7中的解决方案

谢谢,

您可以使用递归:

d1 = {'a': 1, 'b': 2, 'c': [{'d': 3, 'e': 4}, {'d': 5, 'e': 6}]}
def build(d):
  return {f't_{a}':b if not isinstance(b, (dict, list)) else 
           list(map(build, b)) if isinstance(b, list) else build(b) for a, b in d.items()}

print(build(d1))
输出:

{
't_a': 1, 
 't_b': 2, 
 't_c': [
    {'t_d': 3, 't_e': 4}, 
    {'t_d': 5, 't_e': 6}
   ]
}
编辑:要在Python2中运行此解决方案,请使用简单连接替换
f-string

d1 = {'a': 1, 'b': 2, 'c': [{'d': 3, 'e': 4}, {'d': 5, 'e': 6}]}
def build(d):
   return {'t_'+a:b if not isinstance(b, (dict, list)) else 
       list(map(build, b)) if isinstance(b, list) else build(b) for a, b in d.items()}
您可以使用递归:

d1 = {'a': 1, 'b': 2, 'c': [{'d': 3, 'e': 4}, {'d': 5, 'e': 6}]}
def build(d):
  return {f't_{a}':b if not isinstance(b, (dict, list)) else 
           list(map(build, b)) if isinstance(b, list) else build(b) for a, b in d.items()}

print(build(d1))
输出:

{
't_a': 1, 
 't_b': 2, 
 't_c': [
    {'t_d': 3, 't_e': 4}, 
    {'t_d': 5, 't_e': 6}
   ]
}
编辑:要在Python2中运行此解决方案,请使用简单连接替换
f-string

d1 = {'a': 1, 'b': 2, 'c': [{'d': 3, 'e': 4}, {'d': 5, 'e': 6}]}
def build(d):
   return {'t_'+a:b if not isinstance(b, (dict, list)) else 
       list(map(build, b)) if isinstance(b, list) else build(b) for a, b in d.items()}

我正在寻找Python2OK的解决方案。如果键的形式类似于
t_a-a、t_b-b
,则此功能有效。我举了这个例子。但我的原始问题的关键是完全不同的。像
hwPhyAddress-硬件物理地址
@SubhakarKS您希望从
hwPhyAddress-硬件物理地址
得到什么输出?我正在寻找Python2OK中的解决方案。如果键的形式类似于
t_a-a、t_b-b
,则此功能有效。我举了这个例子。但我的原始问题的关键是完全不同的。像
hwPhyAddress-硬件物理地址
@SubhakarKS您希望从
hwPhyAddress-硬件物理地址
输出什么?