如何使用 python 自动制作词典?
How can I automate making dictionaries using python?
我正在做初学者 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,
"homework" : [],
"quizzes" : [],
"tests" : []
}
results.append(names)
return results
list_of_names = ["lloyd", 'alice', 'tyler']
my_dicts = make_dict(list_of_names)
这会跟踪您创建的 names
个指令,然后在最后将它们提供给您。
What is the automated way to make a dictionary, based on lists of
inputs?
你可以在这里使用字典理解。生成器可以避免对样板列表构造代码的需要。在此解决方案中,我们为列表中的每个名称 yield
项。调用 list
然后耗尽生成器,我们可以将列表分配给变量 my_dicts
.
def make_dict(lst):
for name in lst:
d = {k: [] for k in ('homework', 'quizzes', 'tests')}
d['name'] = name
yield d
list_of_names = ['lloyd', 'alice', 'tyler']
my_dicts = list(make_dict(list_of_names))
您正在创建三个词典;但是,每个都通过分配给同一个变量 names
来覆盖前一个,最后一个被垃圾收集,因为对它的唯一引用是一个局部变量,当 make_dict
returns.
您只需要 return 创建 dict
。对于这个练习,听起来你并不真的需要一个循环。
def make_dict(name):
return {
"name": name,
"homework" : [],
"quizzes" : [],
"tests" : []
}
lloyd = make_dict("lloyd")
alice = make_dict("alice")
tyler = make_dict("tyler")
Vegetables={"tamato":40,"carrot":50,"onion":60,"green chillies":20,"red chillies":40,"capsicum":20,"radish":30,"drumstick":40,"beetroot":50,"peas":90}
Print(vegetables)
我正在做初学者 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,
"homework" : [],
"quizzes" : [],
"tests" : []
}
results.append(names)
return results
list_of_names = ["lloyd", 'alice', 'tyler']
my_dicts = make_dict(list_of_names)
这会跟踪您创建的 names
个指令,然后在最后将它们提供给您。
What is the automated way to make a dictionary, based on lists of inputs?
你可以在这里使用字典理解。生成器可以避免对样板列表构造代码的需要。在此解决方案中,我们为列表中的每个名称 yield
项。调用 list
然后耗尽生成器,我们可以将列表分配给变量 my_dicts
.
def make_dict(lst):
for name in lst:
d = {k: [] for k in ('homework', 'quizzes', 'tests')}
d['name'] = name
yield d
list_of_names = ['lloyd', 'alice', 'tyler']
my_dicts = list(make_dict(list_of_names))
您正在创建三个词典;但是,每个都通过分配给同一个变量 names
来覆盖前一个,最后一个被垃圾收集,因为对它的唯一引用是一个局部变量,当 make_dict
returns.
您只需要 return 创建 dict
。对于这个练习,听起来你并不真的需要一个循环。
def make_dict(name):
return {
"name": name,
"homework" : [],
"quizzes" : [],
"tests" : []
}
lloyd = make_dict("lloyd")
alice = make_dict("alice")
tyler = make_dict("tyler")
Vegetables={"tamato":40,"carrot":50,"onion":60,"green chillies":20,"red chillies":40,"capsicum":20,"radish":30,"drumstick":40,"beetroot":50,"peas":90}
Print(vegetables)