美文网首页
2018-11-03总结

2018-11-03总结

作者: smallcatmeat | 来源:发表于2018-11-03 22:45 被阅读0次

如何解决“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)

相关文章

网友评论

      本文标题:2018-11-03总结

      本文链接:https://www.haomeiwen.com/subject/tgplxqtx.html