计算每个位置不同类型的交易数量。蟒蛇

时间:2015-10-21 01:15:17

标签: python count transactions counting

我有一个名为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))

2 个答案:

答案 0 :(得分:1)

这与我的other answer here类似。启动一个字典,然后遍历您的事务列表,并创建一个嵌套字典,其中包含尚未在结果列表中的位置的预初始化值。

class Transaction:
    def __init__(self, time, date ,weekday, duration, amount, _type, location):
            self.time = time
            self.date = date
            self.weekday = weekday
            self.duration = duration
            self.amount = amount
            self.type = _type
            self.location = location

atm_transaction_list = [Transaction(0, 0, 0, 0, 0, 'balance', 'driveup'),
                        Transaction(0, 0, 0, 0, 0, 'withdrawal', 'driveup'),
                        Transaction(0, 0, 0, 0, 0, 'deposit', 'driveup'),
                        Transaction(0, 0, 0, 0, 0, 'advance', 'driveup'),
                        Transaction(0, 0, 0, 0, 0, 'advance', 'driveup'),
                        Transaction(0, 0, 0, 0, 0, 'transfer', 'driveup'),
                        Transaction(0, 0, 0, 0, 0, 'transfer', 'driveup'),
                        Transaction(0, 0, 0, 0, 0, 'withdrawal', 'campus'),
                        Transaction(0, 0, 0, 0, 0, 'deposit', 'campus'),
                        Transaction(0, 0, 0, 0, 0, 'advance', 'campus'),
                        Transaction(0, 0, 0, 0, 0, 'advance', 'campus'),
                        Transaction(0, 0, 0, 0, 0, 'advance', 'campus'),
                        Transaction(0, 0, 0, 0, 0, 'advance', 'campus'),
                        Transaction(0, 0, 0, 0, 0, 'transfer', 'campus'),
                        Transaction(0, 0, 0, 0, 0, 'transfer', 'campus'),
                        Transaction(0, 0, 0, 0, 0, 'transfer', 'campus')]

result = {}
for element in atm_transaction_list:
    if element.location not in result:
        result[element.location] = {'advance':0, 'balance':0, 'deposit':0, 'withdrawal':0, 'transfer':0, 'total':0}
    result[element.location][element.type] += 1

具有以下结果:

>>> result
{'driveup': {'deposit': 1, 'balance': 1, 'advance': 2, 'transfer': 2, 'total': 0, 'withdrawal': 1}, 'campus': {'deposit': 1, 'balance': 0, 'advance': 4, 'transfer': 3, 'total': 0, 'withdrawal': 1}}

你可以整齐地展示:

print('Location   | Advance    |  Balance   |  Deposit   |  Transfer  | Withdrawal |  Total')
for key in result:
    print('{:<10} | {advance:^10} | {balance:^10} | {deposit:^10} | {transfer:^10} | {withdrawal:^10} | {total:^10}'.format(**result[key]))

结果:

Location   | Advance    |  Balance   |  Deposit   |  Transfer  | Withdrawal |  Total
driveup    |     2      |     1      |     1      |     2      |     1      |     0
campus     |     4      |     0      |     1      |     3      |     1      |     0

答案 1 :(得分:0)

您可以使用带有键location_transtype的字典来捕获计数。然后你不必写出每个MxN条件。您只需要弄清楚如何将其重新解释为输出表。