我有一个字典,其中包含'0/RP0': ['NCS1K4-CNTLR']
等条目。对于每个条目,我必须在值中添加另一个字符串。例如,追加'OPERATIONAL'
会产生'0/RP0': ['NCS1K4-CNTLR','OPERATIONAL']
。我需要为完整的字典执行此操作,并有一个要追加的值列表。
我下面有两个词典,
new_dict =
{'0/RP0': ['NCS1K4-CNTLR'], '0/PM1': ['NCS1K4-2KW-AC'], '0/PM0': ['NCS1K4-2KW-AC'], '0/FT2': ['NCS1K4-FAN'], '0/FT0': ['NCS1K4-FAN'], '0/FT1': ['NCS1K4-FAN']}
new_dict_1=
{'0/RP0': ['OPERATIONAL'], '0/PM1': ['OPERATIONAL'], '0/PM0': ['FAILED'], '0/FT2': ['OPERATIONAL'], '0/FT0': ['OPERATIONAL'], '0/FT1': ['OPERATIONAL']}
我想要输出如下,
{'0/RP0': ['NCS1K4-CNTLR','OPERATIONAL'], '0/PM1': ['NCS1K4-2KW-AC','OPERATIONAL'], '0/PM0': ['NCS1K4-2KW-AC','OPERATIONAL'], '0/FT2': ['NCS1K4-FAN','OPERATIONAL'], '0/FT0': ['NCS1K4-FAN','FAILED'], '0/FT1': ['NCS1K4-FAN','OPERATIONAL']}
答案 0 :(得分:2)
提起您的评论,您的第二本字典可以很容易地与第一本字典合并:
dict1 = {'0/RP0': ['NCS1K4-CNTLR'], '0/PM1': ['NCS1K4-2KW-AC'], '0/PM0': ['NCS1K4-2KW-AC'], '0/FT2': ['NCS1K4-FAN'], '0/FT0': ['NCS1K4-FAN'], '0/FT1': ['NCS1K4-FAN']}
dict2 = {'0/RP0': ['OPERATIONAL'], '0/PM1': ['OPERATIONAL'], '0/PM0': ['FAILED'], '0/FT2': ['OPERATIONAL'], '0/FT0': ['OPERATIONAL'], '0/FT1': ['OPERATIONAL']}
for k in dict1.keys():
if k in dict2:
dict1[k].append(dict2[k][0])
print(dict1)
给出:
{'0/RP0': ['NCS1K4-CNTLR', 'OPERATIONAL'], '0/PM1': ['NCS1K4-2KW-AC', 'OPERATIONAL'], '0/PM0': ['NCS1K4-2KW-AC', 'FAILED'], '0/FT2': ['NCS1K4-FAN', 'OPERATIONAL'], '0/FT0': ['NCS1K4-FAN', 'OPERATIONAL'], '0/FT1': ['NCS1K4-FAN', 'OPERATIONAL']}
请注意,dict2[k][0]
会被追加,否则我们会追加一个不是字符串的列表。
答案 1 :(得分:1)
如果你有两个词典,一个带有列表,另一个带有要附加的字符串,你可以使用python dict.keys函数。
dict_of_lists = {...}
dict_to_append ={...}
for key in dict_of_lists.keys():
dict_of_lists[key].append(dict_of_keys[key])
如果第二个dict包含列表,您可能希望使用extent而不是append。
答案 2 :(得分:0)
您可以使用zip()
方法但是已经提到过:字典在Python中是无序的,所以在您需要的代码中可能还有更多内容变化
In [14]: my_dict = {'ak': ['av'], 'bk': ['bv']}
In [15]: my_list = ['a1v', 'b1v']
In [16]: for element in zip(my_dict, my_list):
...: my_dict[element[0]].append(element[1])
...:
In [17]: my_dict
Out[17]: {'ak': ['av', 'a1v'], 'bk': ['bv', 'b1v']}
不要误导结果,也可能是{'ak': ['av', 'b1v'], 'bk': ['bv', 'a1v']}
很可能你需要两个dicts并将它们压缩,而不是像:
In [18]: my_dict = {'ak': ['av'], 'bk': ['bv']}
In [19]: my_dict_2 = {'ak': ['a1v'], 'bk': ['b2v']}
In [20]: for element in zip(my_dict, my_dict_2):
...: my_dict[element[0]].append(my_dict_2[element[1]][0])
...:
In [21]: my_dict
Out[21]: {'ak': ['av', 'a1v'], 'bk': ['bv', 'b2v']}
答案 3 :(得分:0)
字典理解可能比循环更有效。以下方法还通过dict.get
语句替换了对密钥存在的检查,并使用dict1 = {'0/RP0': ['NCS1K4-CNTLR'], '0/PM1': ['NCS1K4-2KW-AC'], '0/PM0': ['NCS1K4-2KW-AC'], '0/FT2': ['NCS1K4-FAN'], '0/FT0': ['NCS1K4-FAN'], '0/FT1': ['NCS1K4-FAN']}
dict2 = {'0/RP0': ['OPERATIONAL'], '0/PM1': ['OPERATIONAL'], '0/PM0': ['FAILED'], '0/FT2': ['OPERATIONAL'], '0/FT0': ['OPERATIONAL'], '0/FT1': ['OPERATIONAL']}
dict1_new = {k: v + dict2.get(k, []) for k, v in dict1.items()}
# {'0/FT0': ['NCS1K4-FAN', 'OPERATIONAL'],
# '0/FT1': ['NCS1K4-FAN', 'OPERATIONAL'],
# '0/FT2': ['NCS1K4-FAN', 'OPERATIONAL'],
# '0/PM0': ['NCS1K4-2KW-AC', 'FAILED'],
# '0/PM1': ['NCS1K4-2KW-AC', 'OPERATIONAL'],
# '0/RP0': ['NCS1K4-CNTLR', 'OPERATIONAL']}
默认值。
database1:
datasource:
repository-package: com.sample.database1.repositories1,com.sample.database1.repositories2
entity-packages: com.sample.database1.entities1,com.sample.database1.entities2