我有一个名为transaction的类,它具有以下属性
Transaction([time_stamp, time_of_day, day_of_month ,week_day, duration, amount, trans_type,
location])
数据集的一个例子就是这样
timestamp time date weekday duration amount trans_type location
1 0:07 3 thu 2 balance driveup
2 0:07 3 thu 6 20 withdrawal campus a
3 0:20 1 tue 2 357 advance driveup
4 0:26 3 thu 2 20 withdrawal campus b
5 0:26 4 fri 2 35 deposit driveup
有不同的交易类型。在事务类型中定义:
advance, balance, deposit, transfer, withdrawal
如何计算交易类型的百分比?你知道吗
例如,这将是结果列表:
[('advance', 20), ('balance', 20), ('deposit', 20), ('transfer', 0), ('withdrawal', 40)]
这就是我尝试过的:
#percentage of the different types of transactions
advance = 0
balance = 0
deposit = 0
transfer = 0
withdrawal = 0
for element in range(len(atm_transaction_list)):
for trans_type in element:
if trans_type == 'advance':
advance += 1
elif trans_type == 'balance':
balance += 1
elif trans_type == 'deposit':
deposit += 1
elif trans_type == 'transfer':
transfer += 1
elif trans_type == 'withdrawal':
withdrawal += 1
使用
for element in range(len(atm_transaction_list)):
,您可以对一个范围内的整数进行迭代。这通常用于处理索引。但是,你不能这么做。只需使用for transaction in atm_transaction_list:
遍历事务列表本身。每个transaction
将成为一个Transaction
对象。你知道吗我还建议将结果存储在词典中,而不是存储在五个单独的参考文献中。然后,您可以在看到某个键时将其添加到该键的值中。你知道吗
这将为您提供一个字典,您可以使用
result['advance']
之类的工具来访问它,以查看'advance'
事务的数量。你知道吗现在将每个键的值除以事务总数,然后乘以100得到百分比:
相关问题 更多 >
编程相关推荐