字典在 visual studio (python) 中不起作用

dict doesn't work in visual studio (python)

我正在学习 cs50 ai python 课程。我正在尝试 运行 涉及大 .csv 文件的代码,因此 cs50 ide 会显示消息 "killed" 而不是 运行 。它通常会 运行小的 csv 文件。所以我将到目前为止的内容复制到 visual studio.VS 中可以毫无问题地加载大 csv 文件,但它给了我错误“'set' object is not subscriptable”

就在这里

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

名字就是这样定义的

# 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: 是来自用户的字符串变量。

如果我将鼠标悬停在名称上,它会显示 (name:dict)

同样的问题

 films =  people[a_id]["movies"]

试试这个

a_id = names[source.lower()]

这会将 a_id 设为 id 秒。

在您的代码中,names 是 name 和 id(集合类型)之间的映射。所以你基本上是在尝试 {'actor': {1, 5, 3}}['actor']['id'].