On 10/06/2011 12:21 PM, lina wrote:
<snip>


As for splitting into functions, consider:

#these two are capitalized because they're intended to be constant
TOKENS = "BE"
LINESTOSKIP = 43
INFILEEXT = ".xpm"
OUTFILEEXT = ".txt"

def dofiles(topdirectory):
    for filename in os.listdr(topdirectory):
        processfile(filename)

def processfile(infilename):
    base, ext =os.path.splitext(fileName)
    if ext == INFILEEXT:
        text = fetchonefiledata(infilename)
        numcolumns = len(text[0])
        results = {}
        for ch in TOKENS:

            results[ch] = [0] * numcolumns
        for line in text:
            line = line.strip()

            for col, ch in enumerate(line):
                if ch in tokens:
                    results[ch][col] += 1

I still have trouble understanding the results[ch][col] part.

Thanks ahead,


First ask yourself what results consists of. It's a dictionary, which is a aggregate of pairs of key/value items. In each case the key is a character, and the the value is a list of numbers.

So results[ch] is a particular value, in other words a list.

And results[ch] [col] is a particular item of the list, in other words, and integer

If we use += 1 on that integer, we increment it by 1

Is that clear? if not, write a separate program to build such as structure, and try printing out each level of item
     print results["E"]
should display a list
      print results["E"][0]
should display an integer
      print results["E"][1]
      print results["E"][2]


Does this help?

--

DaveA

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to