TypeError: list indices must be integers, not str Python

前端 未结 5 479
再見小時候
再見小時候 2020-11-30 15:20

list[s] is a string. Why doesn\'t this work?

The following error appears:

TypeError: list indices must be integers, not str

5条回答
  •  粉色の甜心
    2020-11-30 15:46

    for s in list will produce the items of the list and not their indexes. So s will be 'abc' for the first loop, and then 'def'. 'abc' could only be a key to a dict, not a list index.

    In the line with t fetching the item by index is redundant in python.

提交回复
热议问题