how can we use the same condition by using the iteration for the numbers automatically?
Like 1 after 2. 2 after 3. till 50?

how can we use the same condition by using the iteration for the numbers automatically?
Like 1 after 2. 2 after 3. till 50?

This thread is 3 years old and it is solved. Why don't you start your own thread ? Also please expose your question in details. It's very difficult to understand what you mean.

Like instead of giving BEGIN 1 and Begin 2 separately. can we iterate the number automatically. So that 1 must 2 after the iteration.

Member Avatar for Enalicho

I'm not sure what you're asking. Are you asking if it's possible to have a for loop within a for loop? Sure, it is.
Or are you asking if you can iterate over two objects at once, then the answer is sure, but perhaps not in the way you might think. You use tuple unpacking to assign multiple objects at once.

>>> for x,y in zip(xrange(5),xrange(1,6)):
...     print x, y
...
0 1
1 2
2 3
3 4
4 5
>>> for x,y in zip('ABC','BCD'):
...     print x, y
...
A B
B C
C D

This can be applied to anything zip'able.
If you're not asking either of those, then I'm not sure what you're asking.

Hello everyone,
While extracting a text from a file when we print the 1st line using some condition, how to discard it from parsing to print the next line by using the same condition?

Member Avatar for Enalicho

Well, there are two things to answer that.

Firstly, a boolean object which says whether to print or not.
Secondly, the statement "continue". continue will jump to the next item in the loop, skipping any code beyond the continue.

Example -

found = False
for x in xrange(10):
    if x%3==0:
        if found:
            found = False
            continue
        print x
        found = True

This will print every other number divisible by 3.

If you want to quit the loop after finding the first item, either use a while loop or the break statement, for example -

>>> for x in xrange(10):
...     if x%3==0:
...         print x
...         break
...
0

But my question is to discarding the 1st line after printing it to check the condition for the 2nd line in order to print it. So while printing the 2nd line it should not consider 1st line.

Member Avatar for Enalicho

Err, I'm not quite sure what you mean. Could you give me an example?

for x in xrange(10):
    print x

That does not "consider" the previous line when printing the current line.

Lets take an example of lines containing in a file.
"This is the third largest lake in the world. This is the third largest beach in this country...........................................
He won third prize in quiz"
So if i set the condition as the word "third" it will take 1st line and print it. So while going inside the file to take other information like winning the prize it should not consider the word "third" in 1st line which tells about lakes and beaches.

Member Avatar for Enalicho

So basically, if I'm understanding you, you want to print the last line containing "third" and print it.

Two options -
Parse the file backwards. Start at the last line, break the loop when you find the first line and print it.

Create a variable to store the latest line with "third" in. When a new line is found, update the store. After finishing the loop, print the stored object.

If what you mean is every other line with "third" in, have a look at my other post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.