Python3 Dictionay KeyError: 264

Python3 Dictionay KeyError: 264

我创建了一个字典,其中包含如下内容:

{ Location ID : [[latitude, Longitude], District Name]}

例如,{ 13: [ [40.703278 , -74.017028], 'Manhattan' ]}

现在我正在尝试在另一本词典中创建和整理出租车订单列表。

def trip_distribution(list):
    trips = {}
    for x in list:
        zone = zone_list[x[0]][1]
        if zone not in trips:
            trips[zone] = [x]
        else:
            trips[zone] = trips[zone] + [x]
        return trips

但是当我 运行 代码时,它给了我一个我无法解释的错误: File "test_function.py", line 144, in trip_distribution zone = zone_list[x[0]][1] KeyError: 264

有人知道这是怎么回事吗?

KeyError: 264 表示它无法在字典中找到匹配 264 的键。

很难说哪里出了问题,因为我们不知道 zone_list 和列表中的内容。但是尝试使用 zone = zone_list[x][1]