目录
文章目录
python报错 TypeError:
string indices must be integers
所以在读取字典的时候,最好先判断类型,然后再查看它是否已经有这样的属性:
#检查不是字典
type(mydict) == type({})
如果是字典,再看看有没有这样的属性:
mydict.has_key('mykey')
1、 看看变量是否是字典
2、检查字典是否有对应的key值
if 'like' in condition:
cond_str1 = condition.split('like')[0].strip()
cond_str2 = condition.split('like')[1].strip()
print(staff_info)
ret = filter(lambda x:cond_str2 in str(x[cond_str1]), staff_info) # 这里字典写错
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])
``