gpt4 book ai didi

python - 迭代字典抛出 TypeError : list indices must be integers or slices, 而不是元组

转载 作者:太空宇宙 更新时间:2023-11-03 23:52:40 25 4
gpt4 key购买 nike

我正在尝试遍历我的 dictionary使用以下代码:

num_detections = int(detection['num_detections'])
output_dict = {key:value[0, :num_detections].numpy()
for key,value in detection.items()}

但是我明白了

File "main.py", line 60, in for key,value in detection.items()}

TypeError: list indices must be integers or slices, not tuple

当我打印出 detection它的形状像一本字典和print(type(detection))给我<class 'dict'>

我是不是误会了什么?

最佳答案

列表切片有逗号。我想你想要这个:

num_detections = int(detection['num_detections'])

output_dict = {key:value[0 :num_detections].numpy()
for key,value in detection.items()}

关于python - 迭代字典抛出 TypeError : list indices must be integers or slices, 而不是元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58817559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com