我有一个名为transactions的类,它具有以下属性。你知道吗
transactions([time, date ,weekday, duration, amount, type, location])
这是示例数据类型
time date weekday duration amount type location
0:07 3 thu 2 balance driveup
0:07 3 thu 6 20 withdrawal campusA
0:20 1 tue 2 357 deposit campusB
交易类型为
balance, withdrawal, deposit, advance, transfer
我必须计算不同地点不同类型交易的数量 结果会是这样
Location | Advance | Balance | Deposit | Transfer | Withdrawal | Total
'driveup'| 4 | 191 | 247 | 28 | 530 | 1000
'campus' | 1 | 2 | 3 | 4 | 5 | 15
结果应该发出一个列表,类似于:
[['Location', 'Advance', 'Balance', 'Deposit', 'Transfer', 'Withdrawal', 'Total'],
['driveup', 4, 191, 247, 28, 530, 1000],['campus', 1, 2, 3, 4, 5, 15]]
注意:示例表和结果列表仅显示2个位置。有3个不同的地点driveup“,”campusa“,”campusb“
我怎么列这个单子?你知道吗
我试过这样的方法,但我认为它效率很低,而且有一个更简洁的代码可用,有什么想法吗?你知道吗
amtrlo = lust(zip(amounts, transactions, locations))
for element in amtrlo:
a = element[1]
b = element[2]
c = element[0]
if a == 'advance' and b == 'driveup':
driveup_advance.append((a,b,c))
elif a == 'balance' and b == 'driveup':
driveup_balance.append((a,b,c))
elif a == 'transfer' and b == 'driveup':
driveup_transfer.append((a,b,c))
elif a == 'withdrawal' and b == 'driveup':
driveup_withdrawal.append((a,b,c))
elif a == 'deposit' and b == 'driveup':
driveup_deposit.append((a,b,c))
if a == 'advance' and b == 'campusa':
driveup_advance.append((a,b,c))
elif a == 'balance' and b == 'campusa':
driveup_balance.append((a,b,c))
elif a == 'transfer' and b == 'campusa':
driveup_transfer.append((a,b,c))
elif a == 'withdrawal' and b == 'campusa':
driveup_withdrawal.append((a,b,c))
elif a == 'deposit' and b == 'campusa':
driveup_deposit.append((a,b,c))
if a == 'advance' and b == 'campusb':
driveup_advance.append((a,b,c))
elif a == 'balance' and b == 'campusb':
driveup_balance.append((a,b,c))
elif a == 'transfer' and b == 'campusb':
driveup_transfer.append((a,b,c))
elif a == 'withdrawal' and b == 'campusb':
driveup_withdrawal.append((a,b,c))
elif a == 'deposit' and b == 'campusb':
driveup_deposit.append((a,b,c))
你可以用一个带有键的字典来捕捉计数。这样就不必写出每个MxN条件。您只需找出如何将其重新解释到输出表中。你知道吗
这是以类似于我的other answer here的方式完成的。启动一个字典,然后在事务列表上循环,并为尚未在结果列表中的位置创建一个带有预初始化值的嵌套字典。你知道吗
结果如下:
您可以整齐地显示:
结果:
相关问题 更多 >
编程相关推荐