Python报错:TypeError: list indices must be integers or slices, not str

问题概述

首先报错信息的字面意思为,类型错误:列表索引必须是整数或切片,而不是str
代码如下

# 人体行为信息
person_info = items['person_info']
# 行为信息属性
attributes = person_info['attributes']

print(person_info)
print(attributes)

person_info如下

[{'attributes': {'both_hands_leaving_wheel': {'score': 0.12176866084337, 'threshold': 0.75}, 'eyes_closed': {'score': 0.47068807482719, 'threshold': 0.55}, 'no_face_mask': {'score': 0.99440461397171, 'threshold': 0.75}, 'not_buckling_up': {'score': 0.40850603580475, 'threshold': 0.44}, 'smoke': {'score': 0.0083957426249981, 'threshold': 0.48}, 'cellphone': {'score': 0.2926022708416, 'threshold': 0.69}, 'not_facing_front': {'score': 0.6774777173996, 'threshold': 0.5}, 'yawning': {'score': 0.0052815575618297, 'threshold': 0.5}, 'head_lowered': {'score': 0.18483714014292, 'threshold': 0.55}}, 'location': {'score': 0.97045028209686, 'top': 37, 'left': 64, 'width': 244, 'height': 207}}]

报错信息如下
在这里插入图片描述

分析原因

列表中嵌套了字典,然后字典中也嵌套了字典,为了区分要取列表中的哪个字典,取值的时候要加字典数据的位置索引也就是下标,比如0,1,2之类
,然后加key值,就可以取出想要的数据值了

解决办法

# 人体行为信息
person_info = items['person_info']
# 行为信息属性
attributes = person_info[0]['attributes']

print(person_info)
print(attributes)

成功取出attributes如下

{'both_hands_leaving_wheel': {'score': 0.12176866084337, 'threshold': 0.75}, 'eyes_closed': {'score': 0.47068807482719, 'threshold': 0.55}, 'no_face_mask': {'score': 0.99440461397171, 'threshold': 0.75}, 'not_buckling_up': {'score': 0.40850603580475, 'threshold': 0.44}, 'smoke': {'score': 0.0083957426249981, 'threshold': 0.48}, 'cellphone': {'score': 0.2926022708416, 'threshold': 0.69}, 'not_facing_front': {'score': 0.6774777173996, 'threshold': 0.5}, 'yawning': {'score': 0.0052815575618297, 'threshold': 0.5}, 'head_lowered': {'score': 0.18483714014292, 'threshold': 0.55}}
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风落_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值