python 字典怎么追加键值
#!/usr/bin/envpythontest={}defdict_test(tr):foriintr:test[i['key']]=i['value']returnt...
#!/usr/bin/env python
test = {}
def dict_test(tr):
for i in tr:
test[i['key']] = i['value']
return test
t = [{'key':'z','value':3},{'key':'n','value':1},{'key':'b','value':4}]
r = [{'key':'j','value':6},{'key':'k','value':4},{'key':'f','value':9}]
test_t = {}
test_t['d1'] = dict_test(t)
test_t['d2'] = dict_test(r)
print test_t
#{'d1': {'z': 3, 'b': 4, 'n': 1},'d2': {'k': 4, 'j': 6, 'f': 9}}
要求:上面代码怎么修改能得到注释行的结果? 展开
test = {}
def dict_test(tr):
for i in tr:
test[i['key']] = i['value']
return test
t = [{'key':'z','value':3},{'key':'n','value':1},{'key':'b','value':4}]
r = [{'key':'j','value':6},{'key':'k','value':4},{'key':'f','value':9}]
test_t = {}
test_t['d1'] = dict_test(t)
test_t['d2'] = dict_test(r)
print test_t
#{'d1': {'z': 3, 'b': 4, 'n': 1},'d2': {'k': 4, 'j': 6, 'f': 9}}
要求:上面代码怎么修改能得到注释行的结果? 展开
展开全部
注释行的结果就是程序运行的结果,如果你是python3,将print test_t改为print (test_t)即可
追问
用的是Python2.7,出现是这样结果:{'d2': {'b': 4, 'f': 9, 'k': 4, 'j': 6, 'n': 1, 'z': 3}, 'd1': {'b': 4, 'f': 9, 'k': 4, 'j': 6, 'n': 1, 'z': 3}}
不是想要的
追答
明白你的意思了。将test = {}放到函数内即可。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询