简体   繁体   English

dict 在视觉工作室(python)中不起作用

[英]dict doesn't work in visual studio (python)

I am taking the cs50 ai python course.我正在学习 cs50 ai python 课程。 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.我试图运行涉及 big.csv 文件的代码,因此 cs50 ide 将显示消息“已杀死”而不是运行。它可以正常运行小 csv 文件。 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 "所以我将到目前为止的内容复制到了 Visual Studio。VS 会毫无问题地加载大 csv 文件,但它给了我错误“'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. source:是来自用户的字符串变量。

if I hover over name it says (name:dict)如果我 hover 过名它说(名称:字典)

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.这会将a_id设置为id集。

In your code, names is a map between name and id (which is set type).在您的代码中, names是名称和 id (设置类型)之间的 map。 So you were basically trying things like {'actor': {1, 5, 3}}['actor']['id'] .所以你基本上是在尝试像{'actor': {1, 5, 3}}['actor']['id']类的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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