[Tutor] learning curve
Kent Johnson
kent37 at tds.net
Mon Jan 29 18:04:18 CET 2007
Daniel Klose wrote:
> Thanks Kent,
>
> I now have:
>
> target = sys.argv[1]
>
> seqDictionary = {} # python 2.5 import defaultdict from collections.
> structureArray = [0, 0, 0]
>
> #THIS TAKES THE PLACE OF THE STANDARD PERL $DIR,$file
> #shorter to do the os.path.join once to a variable.
> if (os.path.isfile(os.path.join(structDir, target)) and
> os.path.isfile(os.path.join(seqDir, target))):
> structureHandle = open(os.path.join(structDir, target))
> structureString = structureHandle.readline()
>
> sequenceHandle = open(os.path.join(seqDir, target))
> sequenceString = sequenceHandle.readline()
>
> if len(structureString) == len(sequenceString):
>
> for strChar, seqChar in zip(structureString, sequenceString):
> #SET DEFAULT VALUE AS ZERO ELSE INCREMENT
> seqDictionary[seqChar] = seqDictionary.get(seqChar, 0) + 1
> if (strChar.count('-')):
Since strChar is a single character you can just do
if strChar == '-':
etc.
> structureArray[0] += 1
> elif (strChar.count('H')):
> structureArray[1] += 1
> elif (strChar.count('E')):
> structureArray[2] += 1
> else:
You could put all of the above into a dict lookup, something like
offsets = { '-': 0, 'H': 1, 'E': 2 } # this can be defined outside the loop
offset = offsets.get(strChr)
if offset is None:
print strChar, " is not valid"
else:
structureArray[offset] += 1
Or you could just use another dict for structureArray and index it by
strChar directly.
> print strChar, " is not valid"
> break;
> else:
> print "Some data is missing!\n"
>
> The reason I want to create a dictionary of lists is because for each of
> the keys in the dictionary I wanted to keep tabs on the associated
> structure. For example:
>
> dictionary[A] = [0,0,0]
>
> list x element = A
> list y element = '-'
>
> then dictionary[A][0] = 1
>
> print dictionary[A]
> : [1, 0, 0]
>
> I thought that a dictionary would be the best way (it is the same way as
> I have done it in perl and java). I am using google but having limited
> success.
You can easily make a dict whose values are lists or dicts. I'm not sure
how the above pseudo-code fits in to your code but maybe you want
something like this:
structureArray = structureDict.get(seqChar, [0, 0, 0])
# figure out offset as above
structureArray[offset] = 1 # or += 1 if you are keeping counts.
> *Do you folks bottom post or top post? The users of the perl list are
> sensitive about this stuff!
We're pretty casual here on python-tutors but on comp.lang.python you
will get scolded for top-posting and bottom-posting is more common here
as well.
>
> I am only running python 2.4 and the system admin doesn't like me so I
> won't ask him to upgrade it.
More information about the Tutor
mailing list