如何解决“the_dict = {'x':1,'y':2,'z':3},有一个x_list,需要将the_dict里的x的value代替为x_list中的元素,最终得到一个新的dict的list”
解题关键:
1、在for语句前建立空the_list,否则每次循环时空the_list会覆盖;
2、巧用dict的value替换;
未解决问题:
1、按照代码生成的‘x’的value存在问题。(猜测与对象的引用深度有关)
def get_dict_list(x_list):
the_dict = {'x': 1, 'y': 2, 'z': 3}
the_list=[]
for the_value in x_list:
the_dict['x']=the_value
the_list.append(the_dict)
return the_list
print(the_list)
网友评论