[英]Can't get the walrus operator to work (Python double list comprehension)
此列表理解不起作用:
buy_prices = [(buylow := round(0.997 + ii/10000.0, 5), max(jj, buylow)) for jj in [buylow, 0.9982] for ii in range(21)]
NameError: name 'buylow' is not defined
这个也没有:
buy_prices = [(buylow, max(jj, buylow)) for jj in [buylow := round(0.997 + ii/10000.0, 5), 0.9982] for ii in range(21)]
SyntaxError: assignment expression cannot be used in a comprehension iterable expression
我该怎么做? 还是我只需要对buylow
循环计算?
我强烈建议不要使用嵌套列表理解,因为这对很多人来说可读性不强。
相反,也许是这样的:
buy_prices = list()
for ii in range(21):
buylow = round(.997 + ii / 10000, 5)
for jj in (buylow, .9982):
buy_prices.append((buylow, max(jj, buylow))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.