|
-
May 9th, 2005, 03:12 AM
#1
[Python] For/while loops on lists
I forgot bringing my book with me at work today...grrr...
But here is what I need to do. I have 2 lists. One list has been made by reading in line by line a file. Then I have an other lists with words like this:
[code]
link_types = ["acro",
"also",
"award",
....
"stat".
"trans",
"who",][/list]
And I need to go through every line that doesn't start with a " " and check if one of those words are on the differnt lines. And it has to be fast.
So I was guessing something like this would work:
Code:
print "\nChecking the link types:"
line_number
for line in lines:
if line[0] == " ":
found_it = 0
for types in link_types:
if types in line:
found_it = 1
if found_it == 0:
print line
But here it checks for every word in the list, even if it has all ready found it. If would be much faster if the inner loop was a while loop. But how would I know when it came to the end of the link_types list?
So what I want is something like this:
Code:
print "\nChecking the link types:"
line_number = 0
for line in lines:
line_number = line_number + 1
if line[0] == " ":
found_it = 0
while types in link_types and found_it != 0:
if types in line:
found_it = 1
if found_it == 0:
print "\nLine: " + line_number + line
But I am getting an error on the "while" line. Can't I use while on a list?
-
May 9th, 2005, 06:13 PM
#2
Re: [Python] For/while loops on lists
This may help if you haven't seen it yet...
http://www.python.org/
this is there, too
VB Code:
while_stmt ::= "while" expression ":" suite
["else" ":" suite]
-
May 10th, 2005, 01:21 AM
#3
Re: [Python] For/while loops on lists
Well, yeah that is the syntax of a normal while loop, but it has nothing to do with traversing a list. So it is kind of unusefull.
I am starting to think that it is not possible to traverse through a list using a while loop....
ØØ
-
May 11th, 2005, 09:38 PM
#4
Hyperactive Member
Re: [Python] For/while loops on lists
Code:
for line in lines:
line_number = line_number + 1
if line[0] == " ":
found_it = 0
cindex = 0
while cindex < len(linked_types) and found_it != 0:
if linked_types[cindex] in line:
found_it = 1
cindex=cindex+1
A while loop is not designed to iterate through a list, while a for loop is. This is because a while loop is designed to continue going until certain conditions are met. Thus, you must do things manually. You could also do:
Code:
for line in lines:
line_number = line_number + 1
if line[0] == " ":
found_it = 0
cindex = 0
lt2=linked_types[:]
while len(lt2) and found_it != 0:
types=lt2.pop()
if types in line:
found_it = 1
cindex=cindex+1
Still, the best way of all is:
Code:
print "\nChecking the link types:"
line_number
for line in lines:
if line[0] == " ":
found_it = 0
for types in link_types:
if types in line:
found_it = 1
break
if found_it == 0:
print line
When iterating through a list, use a For loop. Its designed for it. Just use the break statement to break out of it when you so desire.
-
May 12th, 2005, 01:47 AM
#5
Re: [Python] For/while loops on lists
Well, the break is what I am using at the moment. But it looks ugly, at least in 2 nested for loops, then you have to have 2 break, and an if to test if it breaked on the inner one, something like:
Code:
for files_comp in files_and_lines:
for line_comp in files_comp:
if line_comp == line:
found = 1
break
if found == 1:
break
if found == 0:
print "Line: " + line_number
print line
And you are saying that while loops are designed to go on to a condition is met. I know that of course, but I was hoping there was a condition like EOF (End of file) or something for list, since it was so easy to iterate the list with a for loop, but so ugly when you have to use breaks.
But since it doesn't look like that is possible, then I guess I just have to use breaks for list. Grrr....well thanks for the answer.
ØØ
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|