使用字典键和值作为参数并追加到数组

我有两个字典 – 基本字典和额外的,我想使用额外的字典数据与函数交互和追加数据到第一个。

dict_report = {'Id': p_id, 'First name': person_name, 'Age': p_age} #basic dict iter_params = {'Shape': 0, 'Margin': 1} #additional 

我有一个函数接受3个参数,我想使用第二个字典值作为第一个参数。 比方说,在基本的语言:

  def function(): shape = concatvals(iter_params_mammo(['Shape'],14,19)) #goes to cell value directly margin = concatvals(iter_params_mammo(['Margin'], 14, 19)) dict_report.add(shape,margin) return dict_report 

应该返回

 dict_report = {'Id': p_id, 'First name': person_name, 'Age': p_age, 'Shape': "specific shape", 'Margin': "specific margin"} 

我可以以某种方式不指定shapemargin ,而只是附加新的值字典自动,根据额外的字典大小和值,但使用它们的值来创build新的数据?

不知道这是你想要的,但这里是一个粗略的副本。

toda将根据传入的key从现有字典中提取一个值。

arba重新插入密钥。

shimi将更新成为现有的字典,然后更新添加新的键值对

 dict_report = {'Id': 'p_id', 'First name': 'person_name', 'Age': 'p_age'} kv = iter_params = {'Shape': 0, 'Margin': 1} toda = iter_params.get('Shape') arba = {'Shape': toda} print(arba) def dict_merge(x): shimi = dict_report shimi.update(x) print(shimi) return shimi dict_merge(arba) 

我不知道你想创build什么函数或参数,但据我可以告诉你只是想合并一个字典,否则返回它,所以,这里是:

 dict_report = {'Id': "p_id", 'First name': "person_name", 'Age': "p_age"} #basic dict iter_params = {'Shape': 0, 'Margin': 1} def func(d1,d2,arg3): for k,v in d2.items(): #perform function on v #eg v += 1 or whatever you want #add to dict_report d1[k] = v return d1 dict_report = func(dict_report,iter_params,"3rd parameter?") print (dict_report) 

打印:

 {'Id': 'p_id', 'Shape': 0, 'First name': 'person_name', 'Margin': 1, 'Age': 'p_age'}