Re: My first ever Python program, comments welcome

Discussion in 'Python' started by Ivan@work, Jul 23, 2012.

  1. Ivan@work

    Ivan@work Guest

    On 21.07.2012 21:08, Lipska the Kat wrote:
    > Greetings Pythoners
    >
    > A short while back I posted a message that described a task I had set
    > myself. I wanted to implement the following bash shell script in Python
    >
    > Here's the script
    >
    > sort -nr $1 | head -${2:-10}
    >
    > this script takes a filename and an optional number of lines to display
    > and sorts the lines in numerical order, printing them to standard out.
    > if no optional number of lines are input the script prints 10 lines
    >
    > Here's the file.
    >
    > 50 Parrots
    > 12 Storage Jars
    > 6 Lemon Currys
    > 2 Pythons
    > 14 Spam Fritters
    > 23 Flying Circuses
    > 1 Meaning Of Life
    > 123 Holy Grails
    > 76 Secret Policemans Balls
    > 8 Something Completely Differents
    > 12 Lives of Brian
    > 49 Spatulas
    >
    >
    > ... and here's my very first attempt at a Python program
    > I'd be interested to know what you think, you can't hurt my feelings
    > just be brutal (but fair). There is very little error checking as you
    > can see and I'm sure you can crash the program easily.
    > 'Better' implementations most welcome
    >
    > #! /usr/bin/env python3.2
    >
    > import fileinput
    > from sys import argv
    > from operator import itemgetter
    >
    > l=[]


    You can do without this, see below.

    > t = tuple


    This initialization does nothing. Assignment t=(line.split('\t')) makes
    `t` a list (not a tuple), discarding any previous value. And you don't
    really need t:

    > with fileinput.input(files=(filename)) as f:
    > for line in f:
    > t=(line.split('\t'))
    > t[0]=int(t[0])
    > l.append(t)


    List comprehension is your friend, and now you don't need to initialize
    l to an empty list.

    with open(filename) as f:
    l = [line.split('\t') for line in f]

    The first element of each row is now a string, but it's easy to fix:

    > l=sorted(l, key=itemgetter(0))


    Use in-place sorting and cast the sorting element to int

    l.sort(key=lambda t: int(t[0]))


    > inCount = int(argv[2])
    > lineCount = inCount


    lineCount = int(argv[2]) works just fine


    >
    > for c in range(lineCount):
    > t=l[c]
    > print(t[0], t[1], sep='\t', end='')


    Whenever you write "for i in range(n)" you're (probably) doing it wrong.
    Here you can use list slicing, and as a bonus the program doesn't bomb
    when lineCount is greater than length(l)

    for t in l[:lineCount]:
    print(t[0], t[1], sep='\t', end='')
     
    Ivan@work, Jul 23, 2012
    #1
    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. Paul Scott
    Replies:
    4
    Views:
    246
    Paul Scott
    Apr 8, 2008
  2. Ian Foote
    Replies:
    0
    Views:
    156
    Ian Foote
    Jul 21, 2012
  3. MRAB
    Replies:
    4
    Views:
    184
    Dave Angel
    Jul 22, 2012
  4. Dave Angel
    Replies:
    8
    Views:
    253
  5. Peter Otten
    Replies:
    0
    Views:
    162
    Peter Otten
    Jul 22, 2012
Loading...

Share This Page