dict在Visual Studio(python)中不起作用

问题描述 投票:0回答:1

[我正在上CS50 AI Python课程。我正在尝试运行涉及大.csv文件的代码,因此cs50 ide将显示消息“ killed”,而不运行。它将与小csv文件一起正常运行。所以我将到目前为止的内容复制到了Visual Studio中。VS可以毫无问题地加载大型的csv文件,但是它给我的错误是“'set'对象不可下标”

就在这里

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"])

源:是用户的字符串变量。

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

python csv dictionary search visual-studio-2017
1个回答
0
投票

尝试此方法

a_id = names[source.lower()]

a_id设置为id的设置。

在您的代码中,names是名称和ID(设置类型)之间的映射。因此,您基本上是在尝试类似{'actor': {1, 5, 3}}['actor']['id']的事情。

© www.soinside.com 2019 - 2024. All rights reserved.