python - 错误:“int”对象不可下标。***python-jupyter_notebook
问题描述
我试图创建一个接受输入列表(混合类型)的函数,并且只从列表中提取数字。 我不断收到这个 'int' object is not subscriptable 错误。
List = ['M',1,'N',2,'0',3.5]
a_List = [x[:3] for x in List if type(x)!= str]
print(a_List)
解决方案
那时,x
根据条件是一个整数,您不能制作整数切片。尝试x
代替x[:3]
Listt = ['M',1,'N',2,'0',3.5]
a_List = [x for x in Listt if type(x)!= str]
print(a_List)
>> [1, 2, 3.5]