472,097 Members | 1,045 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,097 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. tvshow = {}
  2. start = data.find( '>')
  3. end = data.find( '\n', start+1)
  4. firstname = data[start:end]
  5. start2 = data.find ( '\n', start)
  6. end2 = data.find( '\n', start2 + 1 )
  7. start2 = start2 + 1
  8. lastname = data[start2:end2]
  9. tvshow[firstname] = lastname
  10.  
loop code:
Expand|Select|Wrap|Line Numbers
  1. tvshow = {}
  2.     start = 0
  3.     a = 0
  4.  
  5.     while (data):
  6.         if start != -1:
  7.  
  8.             start = data.find( '>')
  9.             end = data.find( '\n', start+1)
  10.             firstname = data[start:end]
  11.             start2 = data.find ( '\n', start)
  12.             end2 = data.find( '\n', start2 + 1 )
  13.             start2 = start2 + 1
  14.             lastname = data[start2:end2]
  15.             tvshow[firstname] = lastname
  16.             start += 1
  17.             end += 1
  18.             start2 += 1
  19.             end2 += 1
  20.         else:
  21.             break
  22.  
Aug 3 '07 #1
5 1773
ghostdog74
511 Expert 256MB
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:
Expand|Select|Wrap|Line Numbers
  1. tvshow = {}
  2. start = data.find( '>')
  3. end = data.find( '\n', start+1)
  4. firstname = data[start:end]
  5. start2 = data.find ( '\n', start)
  6. end2 = data.find( '\n', start2 + 1 )
  7. start2 = start2 + 1
  8. lastname = data[start2:end2]
  9. tvshow[firstname] = lastname
  10.  
loop code:
Expand|Select|Wrap|Line Numbers
  1. tvshow = {}
  2.     start = 0
  3.     a = 0
  4.  
  5.     while (data):
  6.         if start != -1:
  7.  
  8.             start = data.find( '>')
  9.             end = data.find( '\n', start+1)
  10.             firstname = data[start:end]
  11.             start2 = data.find ( '\n', start)
  12.             end2 = data.find( '\n', start2 + 1 )
  13.             start2 = start2 + 1
  14.             lastname = data[start2:end2]
  15.             tvshow[firstname] = lastname
  16.             start += 1
  17.             end += 1
  18.             start2 += 1
  19.             end2 += 1
  20.         else:
  21.             break
  22.  
how about correcting your indentation first
Aug 3 '07 #2
bvdet
2,851 Expert Mod 2GB
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:
Expand|Select|Wrap|Line Numbers
  1. tvshow = {}
  2. start = data.find( '>')
  3. end = data.find( '\n', start+1)
  4. firstname = data[start:end]
  5. start2 = data.find ( '\n', start)
  6. end2 = data.find( '\n', start2 + 1 )
  7. start2 = start2 + 1
  8. lastname = data[start2:end2]
  9. tvshow[firstname] = lastname
  10.  
loop code:
Expand|Select|Wrap|Line Numbers
  1. tvshow = {}
  2.     start = 0
  3.     a = 0
  4.  
  5.     while (data):
  6.         if start != -1:
  7.  
  8.             start = data.find( '>')
  9.             end = data.find( '\n', start+1)
  10.             firstname = data[start:end]
  11.             start2 = data.find ( '\n', start)
  12.             end2 = data.find( '\n', start2 + 1 )
  13.             start2 = start2 + 1
  14.             lastname = data[start2:end2]
  15.             tvshow[firstname] = lastname
  16.             start += 1
  17.             end += 1
  18.             start2 += 1
  19.             end2 += 1
  20.         else:
  21.             break
  22.  
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.
Aug 3 '07 #3
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:

>Musmusculuslet-7g
UGAGGUAGUAGUUUGUACAGU
>Musmusculuslet-7i
UGAGGUAGUAGUUUGUGCUGU
>MusmusculusmiR-1
UGGAAUGUAAAGAAGUAUGUA

I also practiced by using this:

>Jerry
Seinfeld
>Cosmo
Kramer

what is the problem with the indentation?

Mark
Aug 3 '07 #4
bvdet
2,851 Expert Mod 2GB
I would think a solution like this would be better:
Expand|Select|Wrap|Line Numbers
  1. s = ">Musmusculuslet-7g\nUGAGGUAGUAGUUUGUACAGU\n>Musmusculuslet-7i\nUGAGGUAGUAGUUUGUGCUGU\n>MusmusculusmiR-1\nUGGAAUGUAAAGAAGUAUGUA"
  2.  
  3. dd = {}
  4. sList = s.split()
  5. while len(sList) > 0:
  6.     dd[sList.pop(0)] = sList.pop(1)
  7.  
  8. print dd
Output:
>>> {'>Musmusculuslet-7i': 'UGAGGUAGUAGUUUGUGCUGU', '>MusmusculusmiR-1': 'UGGAAUGUAAAGAAGUAUGUA', '>Musmusculuslet-7g': 'UGAGGUAGUAGUUUGUACAGU'}
>>>
Aug 3 '07 #5
Thanks! That has done the trick

Mark
Aug 3 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

12 posts views Thread by Kamilche | last post: by
9 posts views Thread by No Such Luck | last post: by
17 posts views Thread by Chad Myers | last post: by
7 posts views Thread by Brian Mitchell | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.