Discuss / Python / 第十一天打卡

第十一天打卡

Topic source

方法一:

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没办法去掉

影落潮生y

#2 Created at ... [Delete] [Delete and Lock User]

我也有同样的疑惑,尝试else后用continue跳过,但语法好像不允许。。

前面的if语句找出str然后lower,后面的else语句遍历非str元素


  • 1

Reply