计算类中属性的元素出现百分比。蟒蛇 [英] Counting percentage of element occurence from an attribute in a class. Python
问题描述
我有一个名为transaction的类,它们具有以下属性:
Transaction([time_stamp,time_of_day,day_of_month,week_day,duration, amount,trans_type,
location])
/ p>
时间戳记时间日期周日持续时间量trans_type位置
1 0:07 3 thu 2 balance driveup
2 0 :07 3 thu 6 20撤退校园a
3 0:20 1 tue 2 357提前驾驶
4 0:26 3 thu 2 20撤离校园b
5 0:26 4 fri 2 35存款驱动
有不同的交易类型。在trans_type中定义:
提前,余额,存款,转帐,提款
如何计算交易的百分比类型?
例如,结果列表:
[('advance',20),'balance',20) ),('transfer',0),('withdraw',40)]
我尝试过:
#percentage不同类型的交易
advance = 0
balance = 0
deposit = 0
transfer = 0
撤销= 0
对于范围中的元素(len(atm_transaction_list)):
对于元素中的trans_type:
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 =='提款':
提款+ = 1
解决方案):,你是在一个范围内的整数迭代。这通常在您想使用索引时使用。但是,你不是这样做的。只需对事务列表本身进行迭代,用
对atm_transaction_list:
中的事务进行迭代。每个事务
将是事务
对象。
我还建议将您的结果存储在字典,而不是在五个单独的引用。
result = {'advance':0,'balance':0 ,'deposit':0,'transfer':0,'withdrawal':0}
for atm_transaction_list:
result [element.trans_type] + = 1
这将给你一个字典,你可以使用
result ['advance']
查看'advance'
交易的数量。
现在,将每个键的值除以交易总数,再乘以100即可得到百分比:
l = len(atm_transaction_list)
键入结果:
result [key] = result [key] / l * 100
I have a class called transaction that have these attributes
Transaction([time_stamp, time_of_day, day_of_month ,week_day, duration, amount, trans_type, location])
an example of the data set is as such
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
There are different transaction types. define in trans_type which are:
advance, balance, deposit, transfer, withdrawal
How do I calculate the percentage types of transaction?
For example, this will be the resulting list:
[('advance', 20), ('balance', 20), ('deposit', 20), ('transfer', 0), ('withdrawal', 40)]
This is what i have tried:
#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
解决方案With
for element in range(len(atm_transaction_list)):
, you are iterating over integers in a range. This is generally used when you want to work with indices. However, you're not doing that. Simply iterate over the transaction list itself, withfor transaction in atm_transaction_list:
. Eachtransaction
will then be aTransaction
object.I would also recommend storing your results in a dictionary instead of in five separate references. You can then add to a key's value whenever it's seen.
result = {'advance':0, 'balance':0, 'deposit':0, 'transfer':0, 'withdrawal':0} for element in atm_transaction_list: result[element.trans_type] += 1
This will give you a dictionary that you can access with something like
result['advance']
to see the number of'advance'
transactions.Now divide each key's value by the total number of transactions and multiply by 100 to get the percentage:
l = len(atm_transaction_list) for key in result: result[key] = result[key] / l * 100
这篇关于计算类中属性的元素出现百分比。蟒蛇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!