Newbie- Better code

This is the place for queries that don't fit in any of the other categories.

Newbie- Better code

Postby SCN » Wed Apr 24, 2013 10:59 pm

All,

I am new to python and you will see a lot similar posts from me like this :-). Here is an assignment given

Code: Select all
#
# First we do some setup and create a file with some random numbers
#
with open('/temp/workfile', 'w') as f:
    f.write('69\n39\n32\n59\n50\n49\n82\n40\n58\n40\n43\n7\n50\n58\n13\n89\n58\n68\n32\n20\n17\n37\n24\n96\n83\n67\n30\n41\n54\n58\n17\n54\n98\n37\n43\n10\n68\n44\n65\n12\n78\n98\n96\n44\n25\n22\n44\n63\n41\n31\n92\n87\n53\n37\n96\n71\n19\n1\n56\n75\n82\n73\n9\n63\n86\n45\n38\n21\n4\n13\n55\n70\n48\n34\n63\n69\n21\n93\n36\n85\n56\n68\n18\n70\n68\n92\n63\n87\n25\n66\n74\n30\n68\n33\n21\n24\n41\n55\n51\n30')

#
# Read the file, convert the lines to numbers, and print them out when > 90
#
with open('/temp/workfile', 'r') as f:
    pass # TODO: fix this

Expected output : 96 98 98 96 92 96 93 92


I was able to produce output,but is there a cleaner way? This was my approach
Code: Select all

#
# First we do some setup and create a file with some random numbers
#
with open('/temp/workfile', 'w') as f:
    f.write('69\n39\n32\n59\n50\n49\n82\n40\n58\n40\n43\n7\n50\n58\n13\n89\n58\n68\n32\n20\n17\n37\n24\n96\n83\n67\n30\n41\n54\n58\n17\n54\n98\n37\n43\n10\n68\n44\n65\n12\n78\n98\n96\n44\n25\n22\n44\n63\n41\n31\n92\n87\n53\n37\n96\n71\n19\n1\n56\n75\n82\n73\n9\n63\n86\n45\n38\n21\n4\n13\n55\n70\n48\n34\n63\n69\n21\n93\n36\n85\n56\n68\n18\n70\n68\n92\n63\n87\n25\n66\n74\n30\n68\n33\n21\n24\n41\n55\n51\n30')

#
# Read the file, convert the lines to numbers, and print them out when > 90
#

with open('/temp/workfile', 'r') as f:
    arr=''
    for line in f:
        if int(line) > 90:
         arr = arr + ' '+line.rstrip()
         
    print arr



Thanks
SCN
SCN
 
Posts: 1
Joined: Wed Apr 24, 2013 10:47 pm

Re: Newbie- Better code

Postby Yoriz » Wed Apr 24, 2013 11:37 pm

You could use List comprehension instead of the for loop.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1178
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Newbie- Better code

Postby stranac » Thu Apr 25, 2013 3:24 pm

I wouldn't create the output string like that.

There are at least two better alternatives:

  • Create a list of numbers and the use str.join() to get the wanted formatting:
    Code: Select all
    >>> numbers = ['95', '93', '99']
    >>> print ' '.join(numbers)
    95 93 99

  • Print the numbers as you find them(avoid printing a newline using a comma):
    Code: Select all
    >>> for n in numbers:
    ...     print n,
    ...
    95 93 99
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: Newbie- Better code

Postby Yoriz » Fri Apr 26, 2013 2:45 pm

Feeling generous as the forum has been down, here is some code using list comprehension.
Code: Select all
with open('/temp/workfile', 'r') as f:
    print ' '.join(line.rstrip() for line in f if int(line) > 90)
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1178
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests