计算每个位置不同类型的事务数。python

2024-09-29 17:09:33 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Tags: andiftimeelementtransfertransactionsbalanceelif
2条回答

你可以用一个带有键的字典来捕捉计数。这样就不必写出每个MxN条件。您只需找出如何将其重新解释到输出表中。你知道吗

这是以类似于我的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

相关问题 更多 >

    热门问题