String handling

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PythonNewbie
    New Member
    • Nov 2006
    • 18

    String handling

    Hello, I need some specific string related functionality and I find string functions in Python to be bit lacking. I am not sure if their are other functions that I can use. I did manage to write some code that seems to work but I am afraid it might not work as expected in all cases.

    Lets say, I have following variables,

    myString="RLFEG DNALIR" #some string
    myFrag="NALIR" #known substring of above string

    From these information, I would like to:

    1) Get the
    otherFrag= "RLFEGD" #complimentary fragment/substring of myFrag

    2) Check if their is a specific letter within two spaces of the "fragmentat ion site" in otherFrag

    I could not find any string functions that returned substrings except for split which I was able to make it work for me but it is not satisfactory at all and I am afraid their are bugs from exceptional cases that I might be overlooking. This i what I do right now:

    1)
    otherFrag=st.rs plit(myString,m yFrag,1)[0]

    2)
    proxCheck=(len( otherFrag)-1)-st.rfind(otherF rag,'specific_l etter') #proximity check
    if (proxCheck==1) or (proxCheck==2):
    multipleFlag=0

    I guess I am just wondering if their are more powerful substring methods in python that I can use besides "split" functionality in string. For instance, something that returns substring of a string if I supply the starting and ending position. I suppose I can write a module myself.. hmm
  • dshimer
    Recognized Expert New Member
    • Dec 2006
    • 136

    #2
    I haven't thought about part 2 yet, but the slice method seems to work for part 1.

    Code:
    myString[0:string.find(myString,myFrag)]
    returns 'RLFEGD'
    Originally posted by PythonNewbie
    myString="RLFEG DNALIR" #some string
    myFrag="NALIR" #known substring of above string

    From these information, I would like to:

    1) Get the
    otherFrag= "RLFEGD" #complimentary fragment/substring of myFrag

    Comment

    • dshimer
      Recognized Expert New Member
      • Dec 2006
      • 136

      #3
      Then for part 2, though it is basically just another way of doing what you already have other than getting it in one line.

      Code:
      if specific_letter == otherFrag[len(otherFrag)-1] or specific_letter == otherFrag[len(otherFrag)-2]:
        print 'true'
      Originally posted by PythonNewbie

      2) Check if their is a specific letter within two spaces of the "fragmentat ion site" in otherFrag

      2)
      proxCheck=(len( otherFrag)-1)-st.rfind(otherF rag,'specific_l etter') #proximity check
      if (proxCheck==1) or (proxCheck==2):
      multipleFlag=0

      Comment

      • PythonNewbie
        New Member
        • Nov 2006
        • 18

        #4
        Originally posted by dshimer
        Then for part 2, though it is basically just another way of doing what you already have other than getting it in one line.

        Code:
        if specific_letter == otherFrag[len(otherFrag)-1] or specific_letter == otherFrag[len(otherFrag)-2]:
          print 'true'

        Ok, I think your solution for line 2 is better than mine even though its same concept. I think, unlike my solution, yours would work even if other fragment is only one or two letters long.

        Added later: Although, a more robust solution would be better. I might want to expand the tolerance on proximity and might want to look six spaces - which would mean more if conditions. But for now it works
        Last edited by PythonNewbie; Jan 11 '07, 03:41 PM. Reason: added comment

        Comment

        • dshimer
          Recognized Expert New Member
          • Dec 2006
          • 136

          #5
          In which case, if it were me, I would write an eval function that use something like the following pseudocode.

          Code:
           
          for letter in range(HoweverManyYouWant):
          	if specific letter==letter at that position:
          		return true
          Originally posted by PythonNewbie
          Added later: Although, a more robust solution would be better. I might want to expand the tolerance on proximity and might want to look six spaces - which would mean more if conditions. But for now it works

          Comment

          • ghostdog74
            Recognized Expert Contributor
            • Apr 2006
            • 511

            #6
            Originally posted by PythonNewbie
            1) Get the
            otherFrag= "RLFEGD" #complimentary fragment/substring of myFrag
            How about just replacing with "" judging from your required output.
            Code:
            otherFrag=myString.replace(myFrag,'')
            2) Check if their is a specific letter within two spaces of the "fragmentat ion site" in otherFrag

            I could not find any string functions that returned substrings except for split which I was able to make it work for me but it is not satisfactory at all and I am afraid their are bugs from exceptional cases that I might be overlooking. This i what I do right now:
            substrings are implemented using index slicing in Python. eg astring[0:3] get first 3 characters. judging from your requirements, i think you could just use index slicing.

            Comment

            Working...