标签:包含 整数 类型 most error: for 没有 答案 world
#############>> L = [‘Hello‘, ‘World‘, 18, ‘Apple‘, None]
>> [s.lower() for s in L]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <listcomp>
AttributeError: ‘int‘ object has no attribute ‘lower‘
使用内建的isinstance函数可以判断一个变量是不是字符串:>> x = ‘abc‘
>> y = 123
>> isinstance(x, str)
True
>> isinstance(y, str)
False
###########
答案:
L = [‘Hello‘, ‘World‘, 18, ‘Apple‘, None]
for s in L:
if isinstance(s, str):
L2.append(s)
####################
>> L2 = []
>> for s in L:
... if isinstance(s, str):
... L2.append(s)
...
>> L2
[‘Hello‘, ‘World‘, ‘Apple‘]
标签:包含 整数 类型 most error: for 没有 答案 world
原文地址:http://blog.51cto.com/bobo5620301/2326518