简体   繁体   中英

dict doesn't work in visual studio (python)

I am taking the cs50 ai python course. I was trying to run code that involved big.csv files so the cs50 ide would show the message "killed" and not run.it would run normally with the small csv files. so I copied what i had so far into visual studio.VS would load the big csv files with no problem but it gives me the error " 'set' object is not subscriptable "

right here

a_id = names[source.lower()]["id"]

this is how names was defined

# Maps names to a set of corresponding person_ids
names = {}

# Maps person_ids to a dictionary of: name, birth, movies (a set of movie_ids)
people = {}

# Maps movie_ids to a dictionary of: title, year, stars (a set of person_ids)
movies = {}


def load_data(directory):
    """
    Load data from CSV files into memory.
    """
    # Load people
    with open(f"{directory}/people.csv", encoding="utf-8") as f:
        reader = csv.DictReader(f)
        for row in reader:
            people[row["id"]] = {
                "name": row["name"],
                "birth": row["birth"],
                "movies": set()
            }
            if row["name"].lower() not in names:
                names[row["name"].lower()] = {row["id"]}
            else:
                names[row["name"].lower()].add(row["id"])

source: is a string variable from the user.

if I hover over name it says (name:dict)

same problem here

 films =  people[a_id]["movies"]

Try this instead

a_id = names[source.lower()]

This will set a_id to set of id s.

In your code, names is a map between name and id (which is set type). So you were basically trying things like {'actor': {1, 5, 3}}['actor']['id'] .

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