Discuss / Python / 练习:请修改列表生成式,通过添加if语句保证列表生成式能正确地执行

练习:请修改列表生成式,通过添加if语句保证列表生成式能正确地执行

Topic source

哎呀博雅

#1 Created at ... [Delete] [Delete and Lock User]
L1 = ['Hello', 'World', 18, 'Apple', None]
L2 = [x.lower() for x in L1 if isinstance(x,str)]
# 测试:print(L2)
if L2 == ['hello', 'world', 'apple']:
    print('测试通过!')
else:
    print('测试失败!')

RUN  
['hello', 'world', 'apple']
测试通过!

  • 1

Reply