将列表中的大写字母转换成小写
如果list中既包含字符串,又包含整数,由于非字符串类型没有lower()方法,
l1 = ['hello', 'world', 18, 'apple', none]
请修改列表生成式,通过添加if语句保证列表生成式能正确地执行.
# -*- coding: utf-8 -*- l1 = ['hello', 'world', 18, 'apple', none] l2 = [s.lower() for s in l1 if isinstance(s,str)==true] # 测试: print(l2) if l2 == ['hello', 'world', 'apple']: print('测试通过!') else: print('测试失败!')