[Tutor] learning curve

Daniel Klose perlmunky at googlemail.com
Mon Jan 29 16:13:54 CET 2007


Hi all,

All I would like to do is take a file and count the number of times a
letter occurs in it.  It so happens that there letters are amino acids.
There are also some other checks in the script but these are not a
concern just yet.

What I would like to do is create a dictionary of arrays.
In perl (my current scripting language of choice) I would simply put:
 ${$dictionary{$key}}[$element] += 1
I have no idea how to create this kind of structure in python.

Also I have a while loop.  If this were perl, rather than using the i =
0 while(i < len(x)):
I would do : for (my $i = 0; $i < @array; $i++) {}.  I have found the
range function but I am not sure how to use it properly.
What I would like to do is create an index that allows me to access the
same element in two arrays (lists) of identical size.

I have pasted in my current code below, I would be very grateful if you
could help me trim up this code.
#!/usr/bin/python

import sys, os

structDir = '/home/danny/dataset/structure/'
seqDir   = '/home/danny/dataset/sequence/'

target = sys.argv[1]

seqFile = seqDir      + target
strFile = structDir   + target

seqDictionary = {}

if (os.path.isfile(seqFile) and os.path.isfile(strFile)):
   
    structureHandle = open(strFile)
    structureString = structureHandle.readline()
   
    sequenceHandle  = open(seqFile)
    sequenceString = sequenceHandle.readline()
   
    strArray = list(structureString)
    seqArray = list(sequenceString)

    if len(strArray) == len(seqArray):
        print "Length match\n"
       
        i=0
        while(i < len(strArray)):
            if seqDictionary.has_key(seqArray[i]):
                seqDictionary[seqArray[i]] += 1
            else:
                seqDictionary[seqArray[i]] = 1
               
            i += 1
           
else:
    print "Some data is missing!\n"


More information about the Tutor mailing list