string loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moconno5
    New Member
    • Jul 2007
    • 19

    string loop

    Hello everyone,

    I have a bit of code that I want to repeat for a string. I keep getting infinite while loops when I try it though. Any thoughts on where I am going wrong?

    Thanks,
    Mark

    original code:
    Code:
    tvshow = {}
    start = data.find( '>')
    end = data.find( '\n', start+1)
    firstname = data[start:end]
    start2 = data.find ( '\n', start)
    end2 = data.find( '\n', start2 + 1 )
    start2 = start2 + 1
    lastname = data[start2:end2]
    tvshow[firstname] = lastname
    loop code:
    Code:
    tvshow = {}
        start = 0
        a = 0
        
        while (data):
            if start != -1:
                    
                start = data.find( '>')
                end = data.find( '\n', start+1)
                firstname = data[start:end]
                start2 = data.find ( '\n', start)
                end2 = data.find( '\n', start2 + 1 )
                start2 = start2 + 1
                lastname = data[start2:end2]
                tvshow[firstname] = lastname
                start += 1
                end += 1
                start2 += 1
                end2 += 1
            else:
                break
  • ghostdog74
    Recognized Expert Contributor
    • Apr 2006
    • 511

    #2
    Originally posted by moconno5
    Hello everyone,

    I have a bit of code that I want to repeat for a string. I keep getting infinite while loops when I try it though. Any thoughts on where I am going wrong?

    Thanks,
    Mark

    original code:
    Code:
    tvshow = {}
    start = data.find( '>')
    end = data.find( '\n', start+1)
    firstname = data[start:end]
    start2 = data.find ( '\n', start)
    end2 = data.find( '\n', start2 + 1 )
    start2 = start2 + 1
    lastname = data[start2:end2]
    tvshow[firstname] = lastname
    loop code:
    Code:
    tvshow = {}
        start = 0
        a = 0
        
        while (data):
            if start != -1:
                    
                start = data.find( '>')
                end = data.find( '\n', start+1)
                firstname = data[start:end]
                start2 = data.find ( '\n', start)
                end2 = data.find( '\n', start2 + 1 )
                start2 = start2 + 1
                lastname = data[start2:end2]
                tvshow[firstname] = lastname
                start += 1
                end += 1
                start2 += 1
                end2 += 1
            else:
                break
    how about correcting your indentation first

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Originally posted by moconno5
      Hello everyone,

      I have a bit of code that I want to repeat for a string. I keep getting infinite while loops when I try it though. Any thoughts on where I am going wrong?

      Thanks,
      Mark

      original code:
      Code:
      tvshow = {}
      start = data.find( '>')
      end = data.find( '\n', start+1)
      firstname = data[start:end]
      start2 = data.find ( '\n', start)
      end2 = data.find( '\n', start2 + 1 )
      start2 = start2 + 1
      lastname = data[start2:end2]
      tvshow[firstname] = lastname
      loop code:
      Code:
      tvshow = {}
          start = 0
          a = 0
          
          while (data):
              if start != -1:
                      
                  start = data.find( '>')
                  end = data.find( '\n', start+1)
                  firstname = data[start:end]
                  start2 = data.find ( '\n', start)
                  end2 = data.find( '\n', start2 + 1 )
                  start2 = start2 + 1
                  lastname = data[start2:end2]
                  tvshow[firstname] = lastname
                  start += 1
                  end += 1
                  start2 += 1
                  end2 += 1
              else:
                  break
      Can you show us a sample of the string? In your code, there seems to be no way for the variable 'start' to ever be -1, so the loop would never end. You could break the string up by splitting on '\n' and extract the data by iterating on the list.

      Comment

      • moconno5
        New Member
        • Jul 2007
        • 19

        #4
        Thanks for the replies.
        My thinking was that when there is no data left in the string I am reading (which I read in from a file), then start would equal -1.

        The sample data I am working with is:

        >Musmusculusl et-7g
        UGAGGUAGUAGUUUG UACAGU
        >Musmusculusl et-7i
        UGAGGUAGUAGUUUG UGCUGU
        >Musmusculusm iR-1
        UGGAAUGUAAAGAAG UAUGUA

        I also practiced by using this:

        >Jerry
        Seinfeld
        >Cosmo
        Kramer

        what is the problem with the indentation?

        Mark

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          I would think a solution like this would be better:[code=Python]s = ">Musmusculusle t-7g\nUGAGGUAGUAG UUUGUACAGU\n>Mu smusculuslet-7i\nUGAGGUAGUAG UUUGUGCUGU\n>Mu smusculusmiR-1\nUGGAAUGUAAAG AAGUAUGUA"

          dd = {}
          sList = s.split()
          while len(sList) > 0:
          dd[sList.pop(0)] = sList.pop(1)

          print dd[/code]Output:
          >>> {'>Musmusculusl et-7i': 'UGAGGUAGUAGUUU GUGCUGU', '>Musmusculusmi R-1': 'UGGAAUGUAAAGAA GUAUGUA', '>Musmusculusle t-7g': 'UGAGGUAGUAGUUU GUACAGU'}
          >>>

          Comment

          • moconno5
            New Member
            • Jul 2007
            • 19

            #6
            Thanks! That has done the trick

            Mark

            Comment

            Working...