Discuss / Python / 大神看看什么问题,好吗?

大神看看什么问题,好吗?

Topic source

l1=['Hello','World','18','apple',None] l2=[] for s in l1: if isinstance(s,str)==True : l2.append(s) [q.lower()for q in s]

老衲信你

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

可以直接if isinstance(s, str): 因为if本身就是一个判断了,无需再加 == True 最后一行应该也是可以去掉的

wangruian111

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

l1=['Hello','World',18,'apple',None] l2=[] for s in l1: if isinstance(s,str)==True : l2.append(s) L2=[q.lower() for q in l2]

为什么还要写一个循环,这一节的内容就是让你告别循环啊 L1 = ['Hello','Apple',116,None,'ASSS'] L2 = [s.upper() for s in L1 if isinstance(s,str)] print(L2)

粘错了,不过这个是转换为大写的程序,一样的


  • 1

Reply