nums = ['90']print(all([((ele == ele[::-1] and int(ele) > 0) for ele in nums)]))我期望上面的代码能够打印False,但它正在打印True。您能解释一下打印的原因吗?
1 回答
慕少森
TA贡献2019条经验 获得超9个赞
((ele == ele[::-1] and int(ele) > 0) for ele in nums)
是你的生成器,但是你将其包装在一个 list 中[((ele == ele[::-1] and int(ele) > 0) for ele in nums)]
。因此,您要传递的all
是:
[<generator object <genexpr> at 0x059BE070>]
在这种情况下一切都会返回true
。
你想要的是:
print(all(((ele == ele[::-1] and int(ele) > 0) for ele in nums)))
或者
print(all([(ele == ele[::-1] and int(ele) > 0) for ele in nums]))
添加回答
举报
0/150
提交
取消