Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > REQ: Small Perl to Python conversion needed

Reply
Thread Tools

REQ: Small Perl to Python conversion needed

 
 
Koncept
Guest
Posts: n/a
 
      06-03-2005

Howdie Python folks! I am very new to Python ( 3rd day now ) and it has
already earned its place as my fav. language to work in. I hope to
continue, and I really would appreciate some good resources if anybody
would care to contribute.

My current head-scratcher concerns something I can do in Perl which I
would like to have duplicated for Python. I have noticed that it is not
possible to increment an unset value in Python, so I would like to know
how to duplicate the following bit of code using Python dictionaries.

#!/usr/bin/perl

# Parse comma delimited lines and create a final frequency hash
# Real example would read a file line by line
my %dict = {};
my @lines = ( "1,2,3,4,5", "2,3,4,5", "3,4,5", "4,5", "5" );
foreach(@lines) { map( $dict{ $_ }++, split( "," ) ); }
foreach( sort byKeys keys %dict ) {
print "Key: $_\tFrequency: ", "*" x $dict{ $_ }, "\n"
if $dict{ $_ } =~ /\d+/g;
}
sub byKeys { $dict{$b} <=> $dict{$a} }

__DATA__
Results:
Key: 5 Frequency: *****
Key: 4 Frequency: ****
Key: 3 Frequency: ***
Key: 2 Frequency: **
Key: 1 Frequency: *

--
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
 
Reply With Quote
 
 
 
 
Steven Bethard
Guest
Posts: n/a
 
      06-03-2005
Koncept wrote:
> #!/usr/bin/perl
>
> # Parse comma delimited lines and create a final frequency hash
> # Real example would read a file line by line
> my %dict = {};
> my @lines = ( "1,2,3,4,5", "2,3,4,5", "3,4,5", "4,5", "5" );
> foreach(@lines) { map( $dict{ $_ }++, split( "," ) ); }
> foreach( sort byKeys keys %dict ) {
> print "Key: $_\tFrequency: ", "*" x $dict{ $_ }, "\n"
> if $dict{ $_ } =~ /\d+/g;
> }
> sub byKeys { $dict{$b} <=> $dict{$a} }
>
> __DATA__
> Results:
> Key: 5 Frequency: *****
> Key: 4 Frequency: ****
> Key: 3 Frequency: ***
> Key: 2 Frequency: **
> Key: 1 Frequency: *


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
 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      06-03-2005
Koncept wrote:
> Howdie Python folks! I am very new to Python ( 3rd day now ) and it has
> already earned its place as my fav. language to work in. I hope to
> continue, and I really would appreciate some good resources if anybody
> would care to contribute.
>
> My current head-scratcher concerns something I can do in Perl which I
> would like to have duplicated for Python. I have noticed that it is not
> possible to increment an unset value in Python, so I would like to know
> how to duplicate the following bit of code using Python dictionaries.
>


[expletives deleted]

freq_dict = {}
....
if thing in freq_dict:
freq_dict[thing] += 1
else:
freq_dict[thing] = 1


or, less plainly,

freq_dict[thing] = freq_dict.get(thing, 0) + 1

 
Reply With Quote
 
Ivan Van Laningham
Guest
Posts: n/a
 
      06-03-2005
Hi All--

John Machin wrote:
>
> > how to duplicate the following bit of code using Python dictionaries.
> >

>
> [expletives deleted]
>

+1 QOTW

Metta,
Ivan
----------------------------------------------
Ivan Van Laningham
God N Locomotive Works
http://www.andi-holmes.com/
http://www.foretec.com/python/worksh...oceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      06-03-2005
John Machin wrote:
> freq_dict = {}
> ...
> if thing in freq_dict:
> freq_dict[thing] += 1
> else:
> freq_dict[thing] = 1
>
> or, less plainly,
>
> freq_dict[thing] = freq_dict.get(thing, 0) + 1


or

try:
freq_dict[thing] += 1
except KeyError:
freq_dict[thing] = 1

STeVe
 
Reply With Quote
 
Koncept
Guest
Posts: n/a
 
      06-03-2005
In article <(E-Mail Removed)>, Steven Bethard
<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Conversion of perl based regex to python method Andrew Robert Python 2 05-25-2006 06:25 AM
Conversion of perl unpack code to python - something odd Andrew Robert Python 3 05-18-2006 08:43 PM
Small cameras getting too small? GRL Digital Photography 50 02-03-2006 03:12 AM
Small Square with small red X Peter Coddington Computer Support 4 01-03-2006 06:58 AM
Microsoft small business server 2003 - help needed to understand what is needed to use it Dima Computer Support 5 10-20-2004 08:27 PM



Advertisments