运行 通过带有 for 循环的字符串列表

Running through a list of Strings with a for loop

我想将 subplot_list 的所有内容一一打印出来,但是我收到 list indices must be integers or slices, not str 的类型错误。有什么办法可以绕过这个吗

subplot_list = ['a1', 'a2', 'a3','c1','c2','c3']
for i in subplot_list:
    print(subplot_list[i])

i已经是你要打印的字符串

subplot_list = ['a1', 'a2', 'a3','c1','c2','c3']
for i in subplot_list:
    print(i)

Python 允许您在 for 循环中以两种方式访问​​列表项:

在这里你可以访问项目而不给索引:

subplot_list = ['a1', 'a2', 'a3','c1','c2','c3']
for item in subplot_list:
    print(item)

或索引:

subplot_list = ['a1', 'a2', 'a3','c1','c2','c3']
for i in range(0, len(subplot_list)):
    print(subplot_list[i])
subplot_list = ['a1', 'a2', 'a3','c1','c2','c3']
for i in subplot_list:
    print(i)

#or access the elements with the help of index

for i in range(len(subplot_list)):
    print(subplot_list[i])