我有2个词典:
data1 = [
{key1: 1, key2: 1, count: 3},
{key1: 2, key2: 1, count: 15},
{key1: 3, key2: 1, count: 12},
{key1: 1, key2: 2, count: 13},
{key1: 2, key2: 2, count: 8},
]
data2 = [
key1:{1: 'value1_1', 2: 'value1_2', 3: 'value1_3'},
key2:{1: 'value2_1', 2: 'value2_2'},
]
我想将这两个词典合并为一个大字典,看起来像这样:
new_data = [
{key1: 1, key1_name: 'value1_1', key2: 1, key2_name: 'value2_1', count: 3},
{key1: 2, key1_name: 'value1_2', key2: 1, key2_name: 'value2_1', count: 15},
{key1: 3, key1_name: 'value1_3', key2: 1, key2_name: 'value2_1', count: 12},
{key1: 1, key1_name: 'value1_1', key2: 2, key2_name: 'value2_2', count: 13},
{key1: 2, key1_name: 'value1_2', key2: 2, key2_name: 'value2_2', count: 8},
{key1: 3, key1_name: 'value1_3', key2: 2, key2_name: 'value2_2', count: 0},
]
或者也许,就像那样:
new_data2 = [
{key1: {
value: 1, name: 'value1_1',
data: [
{value: 1, name: 'value2_1', count: 3},
{value: 2, name: 'value2_2', count: 13},
]},
{key1: {
value: 2, name: 'value1_2',
data: [
{value: 1, name: 'value2_1', count: 15},
{value: 2, name: 'value2_2', count: 8},
]},
{key3: {
value: 2, name: 'value1_3',
data: [
{value: 1, name: 'value2_1', count: 12},
{value: 2, name: 'value2_2', count: 0},
]}]
注1:请注意每个合并的字典都有一个额外的行,其中key1 = 3,键2 = 2,这个组合在&#39中没有值; DATA2' dict,因此分配给该行的计数值为0.
注2:我不确定"更好"表示我的数据的方式,如new_data1或new_data2。
注3:我想尽可能地使其通用,我不能假设级别数(在这种情况下是2级,key1和key2)将被修复,所以我需要允许更多级别,这意味着能够处理3级甚至4级。 例如(有3个级别):
data = [
{key1: 1, key1_name: 'value1_1', key2: 1, key2_name: 'value2_1', key3: 1, key3_name: 'value3_1', count: 3},
{key1: 1, key1_name: 'value1_1', key2: 1, key2_name: 'value2_1', key3: 2, key3_name: 'value3_2', count: 2},
{key1: 1, key1_name: 'value1_1', key2: 2, key2_name: 'value2_2', key3: 1, key3_name: 'value3_1', count: 1},
{key1: 1, key1_name: 'value1_1', key2: 2, key2_name: 'value2_2', key3: 2, key3_name: 'value3_2', count: 6},
]
任何帮助将不胜感激。 感谢
答案 0 :(得分:0)
你可以试试这个:
key2_name = "key2_name"
key1_name = "key1_name"
key3_name = "key3_name"
data1 = [
{key1: 1, key2: 1, count: 3},
{key1: 2, key2: 1, count: 15},
{key1: 3, key2: 1, count: 12},
{key1: 1, key2: 2, count: 13},
{key1: 2, key2: 2, count: 8}
]
data2 = { key1:{1: 'value1_1', 2: 'value1_2', 3: 'value1_3'}, key2:{1: 'value2_1', 2: 'value2_2'}
}
for dict_ in data1:
dict_["key1_name"] = data2[key1][dict_[key1]]
dict_["key2_name"] = data2[key2][dict_[key2]]
# equivalent to:
for dict_ in data1:
for k, v in (key1_name, key1), (key2_name, key2):
dict_[k] = data2[v][dict_[v]]
打印(DATA1)
[{'count': 3, 'key2': 1, 'key1': 1, 'key2_name': 'value2_1', 'key1_name': 'value1_1'},
{'count': 15, 'key2': 1, 'key1': 2, 'key2_name': 'value2_1', 'key1_name': 'value1_2'},
{'count': 12, 'key2': 1, 'key1': 3, 'key2_name': 'value2_1', 'key1_name': 'value1_3'},
{'count': 13, 'key2': 2, 'key1': 1, 'key2_name': 'value2_2', 'key1_name': 'value1_1'},
{'count': 8, 'key2': 2, 'key1': 2, 'key2_name': 'value2_2', 'key1_name': 'value1_2'}]
[编辑3键示例]
data1 = [
{key1: 1, key2: 1, key3: 1, count: 3},
{key1: 2, key2: 1, key3: 1, count: 15},
{key1: 3, key2: 1, key3: 1, count: 12},
{key1: 1, key2: 2, key3: 1, count: 13},
{key1: 2, key2: 2, key3: 2, count: 8}
]
data2 = { key1:{1: 'value1_1', 2: 'value1_2', 3: 'value1_3'},
key2:{1: 'value2_1', 2: 'value2_2'},
key3:{1: 'value3_1', 2: 'value3_2'}
}
keys_name = [("%s_name"%v, v) for v in data2.keys()]
for dict_ in data1:
for k, v in keys_name:
dict_[k] = data2[v][dict_[v]]