方法一:
L2 = [s.lower() for s in L1 if isinstance(s, str)]
['hello', 'world', 'apple']
测试通过!
方法二:
L2 = [s.lower() if isinstance (s, str) else s for s in L1]
['hello', 'world', 18, 'apple', None]
测试失败!
None没办法去掉
我也有同样的疑惑,尝试else后用continue跳过,但语法好像不允许。。
前面的if语句找出str然后lower,后面的else语句遍历非str元素
Sign in to make a reply
宇宙计时器
方法一:
L2 = [s.lower() for s in L1 if isinstance(s, str)]
['hello', 'world', 'apple']
测试通过!
方法二:
L2 = [s.lower() if isinstance (s, str) else s for s in L1]
['hello', 'world', 18, 'apple', None]
测试失败!
None没办法去掉