REQ: Small Perl to Python conversion needed

Discussion in 'Python' started by Koncept, Jun 3, 2005.

  1. Koncept

    Koncept Guest

    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
    Koncept, Jun 3, 2005
    #1
    1. Advertising

  2. 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
    Steven Bethard, Jun 3, 2005
    #2
    1. Advertising

  3. Koncept

    John Machin Guest

    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
    John Machin, Jun 3, 2005
    #3
  4. Ivan Van Laningham, Jun 3, 2005
    #4
  5. 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
    Steven Bethard, Jun 3, 2005
    #5
  6. Koncept

    Koncept Guest

    In article <>, Steven Bethard
    <> 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
    Koncept, Jun 3, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joshua
    Replies:
    8
    Views:
    6,040
    Joshua
    Sep 29, 2004
  2. sklett
    Replies:
    1
    Views:
    442
    sklett
    Oct 8, 2007
  3. John

    REQ: Help needed please...

    John, Jul 16, 2004, in forum: Javascript
    Replies:
    4
    Views:
    76
    Hywel
    Jul 17, 2004
  4. darren_uk

    Req.for Advice: Learning Perl

    darren_uk, Feb 6, 2004, in forum: Perl Misc
    Replies:
    5
    Views:
    145
    Randal L. Schwartz
    Feb 9, 2004
  5. Newsgroup User

    REQ: Perl 5.8.3 on OpenBSD

    Newsgroup User, Mar 6, 2004, in forum: Perl Misc
    Replies:
    0
    Views:
    103
    Newsgroup User
    Mar 6, 2004
Loading...

Share This Page