我想用Python完成这个任务。在
您将获得一个包含事务列表的CSV文件示例。在
您的任务是解析CSV,并计算每个帐号的当前余额。在
提交一个接受单个参数(事务CSV文件的位置)的python脚本,并打印出每个帐户的余额。在
CSV文件模式:
from, amount, to
314, 470.21, 275
12, 1788.98, 149
316, 2949.53, 314
5, 2193.48, 454
314, 1402.76, 371
82, 1212.1, 4420
我用以下代码呈现了这个CSV文件:
^{pr2}$如何计算每个帐户的当前余额?在
每个账户都进行多次交易。在
例如:如果帐号314在from
列中,它将给定的金额发送给to
列中的帐号。{cd1>必须在上一列中找到余额。在
如何在for
循环中进行这些计算?在
你可以按照@MrPyCharm的建议使用熊猫。如果只需要清除python解决方案而不需要额外的依赖项,可以这样做:
输出:
^{pr2}$我建议对此使用dictionary,其中键表示帐号。在
其输出如下:
^{pr2}$请注意,事务处理应该始终遵循ACID-Properties。在
这可能不是一个理想的答案,但您可以使用
pandas
来实现这一点。在返回
from
列中每个唯一帐户的总和的列表。在相关问题 更多 >
编程相关推荐