简体   繁体   中英

How can I automate making dictionaries using python?

I am doing a beginners Python course and the aim is to make a bunch of dictionaries.

  1. 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!)

I did this by doing the following:

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)

Why does this not work? Should it work and is it just the Codeacademy development area that does not allow this to work? I realise I am being a little extra and that I could do this really straightforwardly and am purposely trying to be creative in how I do it.

In any case, what is the automated way to make a dictionary, based on lists of inputs?

You're creating a dictionary called names in each loop but not actually doing anything with it --

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)

This keeps track of the names dicts you have created, and then gives them to you at the end.

What is the automated way to make a dictionary, based on lists of inputs?

You can use a dictionary comprehension here. A generator can avoid the need for boilerplate list construction code. In this solution, we yield items for each name in a list. Calling list then exhausts the generator, and we can assign the list to the variable 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))

You are creating three dictionaries; however, each one overwrites the previous one by assigning to the same variable names , and the last one is garbage collected because the only reference to it is a local variable that goes out of scope when make_dict returns.

You just need to return the created dict . For this exercise, it doesn't sound like you really need a loop.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM