REQ: Small Perl to Python conversion needed

Koncept user at unknown.invalid
Fri Jun 3 01:15:00 EDT 2005


In article <0KSdnUXWwbOxOALfRVn-og at comcast.com>, Steven Bethard
<steven.bethard at gmail.com> wrote:

> I don't speak Perl, but based on your output, I'd probably do something 
> like:
> 
> py> lines = ["1,2,3,4,5", "2,3,4,5", "3,4,5", "4,5", "5"]
> py> counts = {}
> py> for items in lines:
> ...    for item in items.split(','):
> ...        counts[item] = counts.get(item, 0) + 1
> ...
> py> for key in sorted(counts, key=counts.__getitem__, reverse=True):
> ...     print 'Key: %s  Frequency: %s' % (key, '*'*counts[key])
> ...
> Key: 5  Frequency: *****
> Key: 4  Frequency: ****
> Key: 3  Frequency: ***
> Key: 2  Frequency: **
> Key: 1  Frequency: *
> 
> I'm probably missing a few subtleties, but hopefully this will get you 
> started.
> 
> STeVe


Thanks Steven. This helped a lot.  Exactly what I was looking for

-- 
Koncept << 
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit."  -Nietzsche



More information about the Python-list mailing list