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
12
solved Counting percentage of element occurence from an attribute in a class. Python