谁能帮我看看我哪里出错了,谢谢
Topic source我上面回复了,可以看看。以下是原答案
python没有三目运算符,可以使用 if else来模拟 v = a if bool b 这样的方式,如果bool为True则返回a否则返回b
L = ['Hello', 'World', 18, 'Apple', None] L_TEST = [s.lower() if isinstance(s,str) else s for s in L] print(L_TEST)
L1 = ['Hello', 'World', 18, 'Apple', None] L2=[s.lower()+str(k) for s in L1 if isinstance(s,str) for k in L1 if not isinstance(k,str)] print(L2)
可能是: 1)str和int不能直接用+号连接; 2)第二个循环中,应该是 for k in L1 if not isinstance(k,str)吧
运行结果为: ['hello18','helloNone','world18','worldNone','apple18','appleNone']
- 1
玖伍北
这个代码是为了在作业基础上同时保留数字和None的,但结果出来是[]。 L1 = ['Hello', 'World', 18, 'Apple', None] L2=[s.lower()+k for s in L1 if isinstance(s,str) for k in L1 if not isinstance(s,str)] print(L2)
请问这个方案为什么行不通。谢谢!