Python:TypeError: list indices must be integers or slices, not str报错解决及原理

问题背景

        有时在写python时,会遇到这个报错

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

        以上报错的意思是  list的索引必须是整数或者片,而不是str

举例

  如下代码

Python:TypeError: list indices must be integers or slices, not str报错解决及原理_第1张图片

分析

我们仔细分析此时的数据,可以发现数据的结构是 列表里套字典,字典里面再套字典。 列表里的字典是要有多个的 ,为了区分是哪个,所以取数据的时候要加字典数据的位置索引0,1之类,再加key值, 就可以取出想要的数据了。

Python:TypeError: list indices must be integers or slices, not str报错解决及原理_第2张图片

解决方法 

        修改后的代码如下,即可解决问题了。

Python:TypeError: list indices must be integers or slices, not str报错解决及原理_第3张图片

你可能感兴趣的:(#,python,python,list,indices,be,integers)