我从更新代码时出错
amount_recieved = 0
for tx in tx_recipient:
if len(tx) > 0:
amount_recieved += tx[0]
减少功能
amount_recieved = functools.reduce(lambda tx_sum, tx_amt: tx_sum + sum(tx_amt[0]) if len(tx_amt) > 0 else 0, tx_recipient, 0)
任何关于这条线出了什么问题的线索都将有助于
reduce
将从您提供的可迭代对象(tx_recipient
(中获取成对的项,因此在lambda中tx_amt
是一个值,因此您不能执行tx_amt[0]
。重写如下:
amount_recieved = functools.reduce(lambda x,y: x+y, tx_recipient)
我假设您的循环只是对列表列表中每个嵌套列表的索引0处的数值求和(其中嵌套列表至少包含一个项(。通过使用functools.reduce()
,您过度简化了单行替换函数以获得相同的结果。你只需要sum()
。例如:
amount_received = sum(tx[0] for tx in tx_recipient if len(tx))