[英]list comprehension for iterating list of dictionaries with bool variables [closed]
在遍历包含 2 个字典的列表时,我注意到 python 的行为非常奇怪,每个字典都有 1 个布尔值。 我希望结果是一致的: False
True
然后[False,True]
在主循环之外的两个列表打印中,但我得到[True,True]
。 有人可以向我解释一下吗? 有没有一种简单的方法可以用 1 行得到 [False,True] ? (而不是使用附加)?
transactions=[{'price_between':False},{'price_between':True}]
for transaction in transactions:
print (transaction['price_between'])
print (list(transaction['price_between'] for transction in transactions))
print ([transaction['price_between'] for transction in transactions])
False
True
[True, True]
[True, True]
你打错了: transction
as transaction
这工作正常:
transactions=[{'price_between':False},{'price_between':True}]
for transaction in transactions:
print (transaction['price_between'])
print (list(t['price_between'] for t in transactions))
print ([t['price_between'] for t in transactions])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.