如何使用python自动创建词典?

如何使用python自动创建词典?,第1张

概述我正在做一个初学者 Python课程,目的是制作一堆词典. Create three dictionaries: lloyd, alice, and tyler. Give each dictionary the keys “name”, “homework”, “quizzes”, and “tests”. Have the “name” key be the name of the stude 我正在做一个初学者 Python课程,目的是制作一堆词典.

Create three dictionarIEs: lloyd,alice,and tyler.

Give each dictionary the keys “name”,“homework”,“quizzes”,and
“tests”.

Have the “name” key be the name of the student (that is,lloyd’s name
should be “Lloyd”) and the other keys should be an empty List (We’ll
fill in these Lists soon!)

我通过执行以下 *** 作来完成此 *** 作:

def make_dict(List_of_names):  for names in List_of_names:    names = {      "name": names,"homework" : [],"quizzes" : [],"tests" : []  }List_of_names = ["lloyd",'alice','tyler']make_dict(List_of_names)

为什么这不起作用?它应该工作,只是Codeacademy开发区域不允许这个工作吗?我意识到我有点多余,而且我可以非常直接地做到这一点,并故意在我如何做到这一点上发挥创意.

在任何情况下,基于输入列表制作字典的自动化方法是什么?

解决方法 你在每个循环中创建一个名为names的字典,但实际上并没有用它做任何事情 –

def make_dict(List_of_names):    results = []    for names in List_of_names:        names = {            "name": names,"tests" : []        }        results.append(names)    return resultsList_of_names = ["lloyd",'tyler']my_dicts = make_dict(List_of_names)

这会跟踪您创建的名称,然后在最后将它们提供给您.

总结

以上是内存溢出为你收集整理的如何使用python自动创建词典?全部内容,希望文章能够帮你解决如何使用python自动创建词典?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1194364.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存