简体   繁体   English

迭代字典会抛出 TypeError:列表索引必须是整数或切片,而不是元组

[英]Iterating on dictionary throws TypeError: list indices must be integers or slices, not tuple

I am trying to iterate over my dictionary with the following code:我正在尝试使用以下代码迭代我的dictionary

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

But I get但我明白了

File "main.py", line 60, in for key,value in detection.items()}文件“main.py”,第 60 行,在 for key,value 在 detection.items()}

TypeError: list indices must be integers or slices, not tuple TypeError:列表索引必须是整数或切片,而不是元组

When I print out detection it's shaped like a dictionary and print(type(detection)) gives me <class 'dict'>当我打印出detection时,它的形状像字典,并且print(type(detection))给了我<class 'dict'>

Am I misunderstanding something?我是不是误会了什么?

There is comma in list slicing.列表切片中有逗号。 I think you wanted this:我想你想要这个:

num_detections = int(detection['num_detections'])

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

暂无
暂无

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

相关问题 遍历字典:类型错误:列表索引必须是整数或切片,而不是 str - Iterating through a dictionary: TypeError: list indices must be integers or slices, not str TypeError:列表索引必须是整数或切片,而不是元组? - TypeError: list indices must be integers or slices, not tuple? TypeError:列表索引必须是整数或切片,而不是元组 - TypeError: list indices must be integers or slices, not tuple 列表类型错误:列表索引必须是整数或切片,而不是元组 - List of lists TypeError: list indices must be integers or slices, not tuple TypeError:列表索引必须是整数或切片,而不是元组列表的元组 - TypeError: list indices must be integers or slices, not tuple for list of tuples 类型错误:列表索引必须是整数或切片,而不是在 python 中使用 sys 导入的元组 - TypeError: list indices must be integers or slices, not tuple with sys import in python 新编码器:TypeError:列表索引必须是整数或切片,而不是元组 - New coder: TypeError: list indices must be integers or slices, not tuple Python棋盘游戏-“类型错误:列表索引必须是整数或切片,而不是元组” - Python Board Game - "TypeError: list indices must be integers or slices, not tuple" TypeError:列表索引必须是整数或切片,而不是电影分级数据的元组 - TypeError: list indices must be integers or slices, not tuple for Movie Rating Data Python 类型错误:列表索引必须是整数或切片,而不是元组 - Python TypeError: list indices must be integers or slices, not tuple
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM