问题
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
回答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, with for transaction in atm_transaction_list:
. Each transaction
will then be a Transaction
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
来源:https://stackoverflow.com/questions/33213843/counting-percentage-of-element-occurence-from-an-attribute-in-a-class-python