尝试在列表理解中使用 if else 预期输出为 (23.0,23.3,26.0,27.0,0) 已尝试将值转换为 float ,然后再将其潜水为十,但仍然不起作用
temps= (230,233,260,270,-999)
new_temps=((temp)/10 if temp != -999 else 0 for temp in temps)
print(new_temps)
我得到的错误是 <0x0000021889021040处的生成器对象>
最佳答案
列表理解是通过[]
完成的,而不是()
,后者实际上是创建generator的符号。 .
要解决这个问题,只需更改为 []
:
temps= (230,233,260,270,-999)
new_temps=tuple([(temp)/10 if temp != -999 else 0 for temp in temps])
print(new_temps)
但是如果您确实想使用生成器表示法,也可以这样做:
temps= (230,233,260,270,-999)
new_temps=tuple((temp/10 if temp != -999 else 0 for temp in temps))
print(new_temps)
关于python - '<生成器对象 <genexpr> 位于 0x0000014A76371040>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66684470/