473,379 Members | 1,491 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 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 1828
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

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

Similar topics

12
by: Kamilche | last post by:
I was looking for a way to speed up detecting invalid characters in my TCP string, and thought of yet another use for the translate function! If you were to 'translate out' the bad characters, and...
9
by: No Such Luck | last post by:
I have a function which requires me to loop from the end of a string to the beginning on a char by char basis: int foo (char string) { unsigned int i; for(i = strlen(string); i >= 0; i--) {...
19
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
35
by: Cor | last post by:
Hallo, I have promised Jay B yesterday to do some tests. The subject was a string evaluation that Jon had send in. Jay B was in doubt what was better because there was a discussion in the C#...
7
by: Brian Mitchell | last post by:
Is there an easy way to pull a date/time stamp from a string? The DateTime stamp is located in different parts of each string and the DateTime stamp could be in different formats (mm/dd/yy or...
7
by: bcpkh | last post by:
Hello All I need to check a string to make sure it does not contain any non numeric characters, the problem that I face is that the string is fairly long, 2784601121574585949, strtol etc. can't...
1
by: kellysgirl | last post by:
Now what you are going to see posted here is both the set of instructions I was given..and the code I have written. The instructions I was given are as follows In this case, you will create...
4
by: chellemybelle | last post by:
Hello, I basically have made a little cheezy slideshow and would like to add captions to each pic as it loops through. I've tried everything I can think of. I'm assuming I might need to use an...
3
by: yogi_bear_79 | last post by:
I'm sure I have a few things wrong here. But I am stuck on how to do a recurring search. Also my statement cin >quote; acts weird. If I enter more than one word it blows right past cin >findMe;...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.