Python 字符串生成数组list报错:TypeError: ‘type‘ object is not subscriptable

>>> a = '1'
>>> list[a]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'type' object is not subscriptable
>>> [a]
['1']

请添加图片描述

生成数组的时候,无需加上 ‘list’ 的方法,直接用 中括号就可以了

解析:
subscriptable的意思是 可有下标的 意思
错误的原因就是把不具有下标操作的对象用成了对象[i],比如int对象变量[i]就会报错。仔细检查错误行。

此处是将string 转换成 list ,无效。除非是

>>> a = '1234'
>>> list(a)
['1', '2', '3', '4']
>>>

猜你喜欢

转载自blog.csdn.net/dare_kz/article/details/124269729