Sunday 15 April 2012

Python: Printing Unique Value in Dictionary along with Count -



Python: Printing Unique Value in Dictionary along with Count -

i'm working on extension of logging system. i'm writing function such when user runs code, take @ run logs, , if there errors collect them along run log itself.

i've been able , have dict of run logs , error messages contain, errors duplicated across multiple logs i'd give user 'sample' log each error message, along amount of errors find.

my dict like

{ 'run_log_1': 'syntax error on abc', 'run_log_3': 'syntax error on abc', 'run_log_17': 'expected int base of operations 10', 'run_log_23': 'syntax error on xyz' }

you idea.

i'd format output such user gets:

error: syntax error on abc total 2, sample: run_log_3 error: expected int base of operations 10 total 1, sample: run_log_17

how go this?

you can create utilize of of special collections task.

import collections your_log_dict = dict( run_log_1='syntax error on abc', run_log_3='syntax error on abc', run_log_17='expected int base of operations 10') counts = collections.counter(your_log_dict.values()) samples = {v:k k,v in your_log_dict.items()} error, count in counts.items(): sample = samples[error] print('error: {}\ntotal {}, sample: {}\n'.format(error, count, sample))

python dictionary

No comments:

Post a Comment