Problem with a tuple - newbie ignorance

Alan Daniels danielsatalandanielsdotcom
Tue Jan 16 00:00:48 EST 2001


On Mon, 15 Jan 2001 15:23:14 -0500, Steven Citron-Pousty
<steven.citron-pousty at yale.edu> wrote:

>Here is my code - remember think newbie and don't slam me too hard, its
>one of those days. Thanks again for any help.

Other posts have already pointed out the difference between list
copying and list referencing, so I won't belabor that, but here's a
couple of idioms you can use to make your code more Pythonic if you
wish:

1) Multiple imports....
>import os
>import sys
>import string
>import re
...can be written in one line, such as...

    import os, sys, string, re

This is a matter of style and strictly up to you. Either way works
fine.

2) Like others have said, the "ifields" data structure looks as if it
would be better off as a dictionary, thus giving you nice key/value
lookup capability. Plus you wouldn't have to hardcode that "numfields"
value! Let me know if you want help on this and I'll help you offline.

3) If you find yourself doing the double-backslash a lot (thanks,
Microsoft!), rather than typing...
> dir = os.listdir('D:\\statlab\\ssda\\data')
...you can write this as...

  dir = os.listdir(r'D:\statlab\ssda\data')

The 'r' before the string means it's a "raw" string, and all escape
sequences are ignored. Handy for long Windows file paths.

4) If you ever find yourself writing a line like...
> if (i.rfind(j)==0) or (i.rfind(j)==1) or (i.rfind(j)==3):
...where you test to see if a value is one of many possible discrete
values, remember that tuples are your friends, and try:

  if i.rfind(j) in (0, 1, 3):

...instead. The "in" operator lets you test for memebership of an item
in a sequence (i.e. is a value in a certain list, or tuple), or even
looking for a character in a string. Plus, your code will run faster
since "rfind" will be called *one* time instead of three.

5) If you ever need to do the typical "for i = 1 to 10" - style
enumerating through a number, such as you have here...

>    crap  = 0
>    while crap < numfields :
>        if result[1][crap]:
>            print "field # ", result[0][crap], " ", result[1][crap]
>        crap += 1

...you should replace it with a "for" statement using a call to
"range" (this generates a list of numbers for you). Example:

     for crap in range(numfields):
         if result[1][crap]:
             print "field # ", result[0][crap], " ", result[1][crap]

6) Python has built in "sprintf"-style string interpolation using the
percent-sign character on a string, turning...
> print "field # ", result[0][crap], " ", result[1][crap]
...into...

  print "field # %s %s" % (result[0][crap], result[1][crap])

...but once again, this is a matter of style. I generally use the
percent style interpolation whenever I can, because otherwise the
string handling looks too much like Java, and I get enough of *that*
language at work. :-)

This doesn't cover it all (I'm sure there's more), but I hope this
helps.
==============================
Alan Daniels
daniels at alandaniels dot com



More information about the Python-list mailing list