Python经验总结:两种Type Error问题
更新时间:2023年09月09日 11:11:37 作者:Big_quant
这篇文章主要介绍了Python经验总结:两种Type Error问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
python报错 TypeError:
string indices must be integers
所以在读取字典的时候,最好先判断类型,然后再查看它是否已经有这样的属性:
#检查不是字典
type(mydict) == type({}) 如果是字典,再看看有没有这样的属性:
mydict.has_key('mykey') 1、 看看变量是否是字典
2、检查字典是否有对应的key值
list indices must be integers or slices, not tuple
以下两种情况都会出现此错误:
points = [ [1, 2], [0, 4], [2, 0][12,1]]
list里的元素必须一样:
points = [ [1, 2],[0, 4],[2, 0]]
这个也会报错:
stations = ['Schagen', 'Heerhugowaard', 'Alkmaar', 'Castricum', 'Zaandam', 'Amsterdam', 'Sloterdijk',
'Amsterdam Centraal', 'Amsterdam Amstel', 'Utrecht Centraal', ''s-Hertogenbosch', 'Eindhoven', 'Weert',
'Roermond', 'Sittard', 'Maastricht']
IndEind = stations.index("Heerhugowaard")
IndBegin = stations.index('Sloterdijk')
intBegin = int(IndBegin)
intEind = int(IndEind)
print('stations[0]: ', stations[intBegin, intEind])这个是因为读取的是时候维数错误:
正确写法:
print('stations[0]: ', stations[intBegin:intEind])
``总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
python+pytest接口自动化之日志管理模块loguru简介
python中有一个用起来非常简便的第三方日志管理模块--loguru,不仅可以避免logging的繁琐配置,而且可以很简单地避免在logging中多进程多线程记录日志时出现的问题,甚至还可以自定义控制台输出的日志颜色,接下来我们来学习怎么使用loguru模块进行日志管理2022-05-05
Opencv中cv2.cvtColor彩色图转灰度图的其他6种方法
本文主要介绍了Opencv中cv2.cvtColor彩色图转灰度图的其他6种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2022-05-05


最新评论