Discuss / Python / 交作业

交作业

Topic source

o0stanley0o

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

L2 = [x.lower() for x in L1 if isinstance(x, str) == True]  #不保留数字

L3 = [x if isinstance(x, str) == False else x.lower() for x in L1]   #保留数字不处理

o0stanley0o

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

L2 = [x.lower() for x in L1 if isinstance(x, str) == True]  #不保留数字

L3 = [x.lower() if isinstance(x, str) else x for x in L1]   #保留数字不处理

---------------------------

这个更简洁一点……

o0stanley0o

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

L2 = [x.lower() for x in L1 if isinstance(x, str)]  #不保留数字

L3 = [x.lower() if isinstance(x, str) else x for x in L1]   #保留数字不处理

---------------------------

贴错了,重新发。。。话说不能删除回复,好不方便啊!

#?头保命 

按照L3的写法,None会被保留

最后加个判断 if s  可以把None 去掉。

L3 = [s.lower() if isinstance(s, str) else s for s in L1 if s]

  你刷新了我对这个语句的观念!


  • 1

Reply