简体   繁体   English

如何用来自同一键值对的值替换嵌套字典中的键值对?

[英]How to replace a key-value pair in a nested dictionary with the value from the same key-value pair?

I want to replace the key-value pair in a directory with the value from the same key-value pair.我想用来自同一键值对的值替换目录中的键值对。 In simple words, I want to replace简单来说,我想替换

{k1:{key1:value1},k2:{Key2:value2}} with {k1:value1,k2:value2}

in simple words: {key1:value1} gets replaced with value1简单来说: {key1:value1}被替换为 value1

My input is {'times': {0: 3284566678.449864},
 'C0PLUSA': {0: 2.4529042386185244e-06},
 'C1PLUSA': {0: 2.215571719760855e-06},
 'I0PLUSA': {0: 1200.000000000029},
 'M1BA': {0: 1.1971391933999447e-05},
 'M1DA': {0: 2.5217060699404884e-06},
 'M1GA': {0: 1.0922075194286918e-05},
 'M1SA': {0: 0.0006776339108859284},
 'M2BA': {0: 1.7152638293476106e-05}}

code:代码:

for i, key,value in a.items:
 a[key][value]=value.values()

but i get an error saying this object is not iterable.但是我收到一个错误,说这个对象不可迭代。

for node,value in a.items():
a = a.replace(node.values(),value.values())

but it still gives me an error I am expecting to get an output as但它仍然给我一个错误,我希望得到一个输出

{'times':3284566678.449864,
     'C0PLUSA': 2.4529042386185244e-06,
     'C1PLUSA': 2.215571719760855e-06,
     'I0PLUSA': 1200.000000000029,
     'M1BA':  1.1971391933999447e-05,
     'M1DA':  2.5217060699404884e-06,
     'M1GA':  1.0922075194286918e-05,
     'M1SA':  0.0006776339108859284,
     'M2BA':  1.7152638293476106e-05}

If all the keys in the inner dictionaries are 0 you can just do:如果内部字典中的所有键都是0你可以这样做:

{k:v[0] for k,v in data.items()}

In case you want to ignore the keys of the inner dictionaries without knowing the value, you can see how this example works:如果你想在不知道值的情况下忽略内部字典的键,你可以看看这个例子是如何工作的:

>>> data={'k1': {'key1': 'value1'}, 'k2': {'key2': 'value2'}}
>>> {k:list(v.values())[0] for k,v in data.items()}
{'k1': 'value1', 'k2': 'value2'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python:如何在嵌套字典的开头添加键值对? - python: how to add a key-value pair to the beginning of a nested dictionary? 给定键值对中的已知值,请提取字典中下一个键值对的值 - Given a known value in a key-value pair, extract the value of the next key-value pair in a dictionary 如何处理该词典中的每个日期键值对? - How to process every date key-value pair inside this dictionary? 如何互换使用 python 字典键值对? - How to use python dictionary key-value pair interchangeably? 如果字符串末尾包含“:”,如何用我的键值对替换字典键 - how to replace my dictionary key with my key-value pair if that contains “ : ” at the end of the string Python 3 如何从只包含一个未知键的键值对的字典中获取值? - Python 3 how to get value from dictionary containing only one key-value pair with unknown key? 如何从 python 的字典中的另一个键值对中弹出一个键值对 - How to pop out a key-value pair from within a another key value pair in dictionaries in python 如何检查字典中是否存在键值对? 如果一个键的值是字典 - How do I check if a key-value pair is present in a dictionary? If the value of one key is a dictionary 使用布尔键值对将字符串转换为字典格式 - Convert string to dictionary format with boolean key-value pair 在字典的开头添加新的键值对 - Add new key-value pair at the beginning of a dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM