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常用内置模块日志、随机数、时间、OS与JSON详解
本文介绍了Python开发中常用的五大模块:日志、随机数、时间、系统文件操作及JSON数据处理,详细讲解了各模块的的常见函数及其应用场景,需要的朋友可以参考下2026-05-05
详解django+django-celery+celery的整合实战
这篇文章主要介绍了详解django+django-celery+celery的整合实战,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-03-03


最新评论