为什么这个代码不行? L1 = ['Hello', 'World', 18, 'Apple', None] L2=[s.lower() for s in L1 if isinstance(s,str)=='Ture'] print(L2) 我看别人是L2 = [s.lower() for s in L1 if isinstance(s, str)],不需要判断isinstance(s, str)的结果吗?那要是希望留下False的一部分怎么办?
还有你们的代码运行助手都可以用吗?为啥我的不行?
把'True'单引号去掉 if isinstance(s, str) 本身包含了判断, 只有当它判断为True,它才会将这个值带进去
知道了,谢谢。
Sign in to make a reply
星月starnight
为什么这个代码不行? L1 = ['Hello', 'World', 18, 'Apple', None] L2=[s.lower() for s in L1 if isinstance(s,str)=='Ture'] print(L2) 我看别人是L2 = [s.lower() for s in L1 if isinstance(s, str)],不需要判断isinstance(s, str)的结果吗?那要是希望留下False的一部分怎么办?
还有你们的代码运行助手都可以用吗?为啥我的不行?