使用内建的isinstance函数可以判断一个变量是不是字符串:
1 2 3 4 5 6
|
>>> x = 'abc' >>> y = 123 >>> isinstance(x, str) True >>> isinstance(y, str) False
|
列表生成式 + if
1 2 3
|
>>> L1 = ['Hello', 'World', 18, 'Apple', None] >>> [data.lower() for data in L1 if isinstance(data,str)] ['hello', 'world', 'apple']
|
近期评论