By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,183 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

string loop

P: 19
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
Share this Question
Share on Google+
5 Replies


Expert 100+
P: 511
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
Expert Mod 2.5K+
P: 2,851
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

P: 19
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
Expert Mod 2.5K+
P: 2,851
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

P: 19
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.