当前位置:网站首页>TypeError:列表索引必须是整数或片,不是str
TypeError:列表索引必须是整数或片,不是str
2022-08-05 19:58:00 【no envy】
TypeError:List indices must be integers or slices , not str
报错代码
A friend in a fan group,An error is reported for complex dictionary values,He wants to implement printingpeach:
import json
python_str={
"season":
[
{
"spring":[{
"fruit":"pineapple"}]},
{
"summer":[{
"fruit":["watermelon","peach"]}]},
{
"fall":[{
"fruit": "apple"}]},
{
"winter":[{
"fruit": "orange"}]}
]
}
#Locate data nodes
#print dictionary allkey
print(python_str.keys())
#print dictionary allvalue
print(python_str.values())
print(python_str["season"]["spring"])
报错信息截图:

报错翻译
报错信息翻译:
类型错误:List indices must be integers or slices,而不是str
报错原因
Observing the code code, you can find that the highlighted red box indicates that the value cannot be obtained

报错原因:The code is wrong and cannot get the value
解决方法
For complex dictionaries, it is necessary to fetch down layer by layer,修改代码为:
import json
python_str={
"season":
[
{
"spring":[{
"fruit":"pineapple"}]},
{
"summer":[{
"fruit":["watermelon","peach"]}]},
{
"fall":[{
"fruit": "apple"}]},
{
"winter":[{
"fruit": "orange"}]}
]
}
print(python_str["season"][1]['summer'][0]['fruit'][1])
边栏推荐
- HCIP第十七天笔记(接口状态的变化、STP配置、802.1W的改进点、MSTP)
- SwiftUI case: 3D rotating picture player
- JS高阶(二)Symbol
- Taishan OFFICE Technical Lecture: When the same borders are adjacent to each other, it is reasonable and unreasonable to merge
- C# implements the singleton pattern and implementation ideas
- 极光推送之自定义声音踩坑记录(持续更新)
- 如何更换war和jar包中的文件
- Rust Study Notes: 3.1 Variables and Mutability
- VMware - clone virtual machine
- 142. 环形链表 II-双指针法
猜你喜欢

【ML】机器学习数据集:sklearn中回归数据集介绍

加密熊市为企业并购提供机遇!野心勃勃or救世主?唯一真理便是利益至上!

Pytest学习-Fixture最佳使用方式

【代码解读】超详细,YOLOV5之build_targets函数解读。

报告分享|2022年深度学习平台发展报告PPT

03 数据库查询、模型查询、多库查询《ThinkPHP6 入门到电商实战》

JVM参数配置说明

第04篇:Guava-retry重试组件

HCIP第十七天笔记(接口状态的变化、STP配置、802.1W的改进点、MSTP)

Codeforces: d. Chip Move "dp + reverse thinking to consider"
随机推荐
【开发者必看】【push kit】推送服务典型问题合集2
Detailed explanation of RAID disk array
【StoneDB子查询优化】subquery子查询-exists子查询的剔除遍历处理
C#实现单例模式和实现思路
编译器工程师眼中的好代码:Loop Interchange
【StoneDB模块介绍】服务器模块
IPv4和IPv6有什么区别
SwiftUI案例:3D旋转图片播放器
SwiftUI案例:天气
方舟:生存进化开服服务器配置如何选择?
Pytest Learning - The Best Way to Use Fixture
面试题 02.07. 链表相交-双指针法
第06篇:池化技术
方舟开服务器怎么开
泰山OFFICE技术讲座:按照WORD的做法,底纹高亮边框的效果示意图
Vim命令总结
CyclicBarrier
TensorFlow learning record (4): stochastic gradient descent & Keras high-level interface
软件测试面试(五)
27. Remove elements - double pointer method