Question: Difficulty in python while loop
0
KVC_bioinfo410 wrote:

Hello All,

I wrote following python code:

``````pos = 100
cigar = [(0, 150), (3, 50), (0, 130), (2, 270), (0, 750),(2,50),(3, 70), (0,500)]
result=list()
for i in cigar:
while i == 0:
if i == 3:
break
end = pos + i
item = [i, pos, end]
#print item
result.append(item)
pos=end

matches=list()
for i in result:
if i<1:
matches.append([i, i])
#print matches
``````

Here I am trying to generate an output which will give me:

``````[100, 100+150]  i.e [100, 250] and so on

[100+150+50, 100+150+50+130+270+750+50]

[100+150+50+130+270+750+50+70, 100+150+50+130+270+750+50+70+500]
``````

So basically starting from the pos which are equal to 100: it should keep adding the second value from each cigar tuple (0, 150) until a cigar tuple come where the first value is 3 (3, 50) and keep doing it until the end of cigar list.

I have used while loop for this. However, there is something wrong with the loop because it does not give the desired output and it never breaks. Could anyone figure out what am I doing wrong here?

Thank you very much in advance.

whileloop python • 895 views
modified 24 months ago • written 24 months ago by KVC_bioinfo410
1

Well, if you say "So basically starting from the pos which are equal to 100" that makes no sense to me, because I can not see 100 in your data at all. However, talking about your loop..... It is not surprising, that it never breaks: You check for i equals 3. However, i is the elements of "cigar" which are (0,50), (3, 50) .... etc, but NEVER 3 .... just add a print i after "for i in cigar:" and you will see the value of i. It can not be 3, ever.

100 is the "pos" which are the first starting point

Ah sorry, I was too focused on the 'cigar'. Anyway, I hope you understood the point about not reaching the breaking point, as Devon described in more detail below.

However, there is something wrong with the loop because it does not give the desired output and it never breaks

What do you get?

2
Devon Ryan93k wrote:

`i == 3` can never be true, because `i` is a tuple. You'll want to test `i == 3` instead. Anyway, below is presumably what you were going for:

``````pos = 100
end = 0
cigar = [(0, 150), (3, 50), (0, 130), (2, 270), (0, 750),(2,50),(3, 70), (0,500)]
results = list()
for op, oplen in cigar:
if op == 3:
# Flush the memoized values
results.append([pos, end])
pos = end + oplen
end = pos
else:
end += oplen

# It's not entirely clear what should happen if the last CIGAR op is a 3
results.append([pos, end])
for tup in results:
print(tup)
``````
``````cigar =  [(0, 150),(3, 50), (0, 130), (2, 270), (0, 750),(2,50),(3, 70), (0,500)]
``````

Starting from 100 which is "pos" in the code the output should look like:

``````[100, 100+150]

[100+150+50, 100+150+50+130+270+750+50]

[100+150+50+130+270+750+50+70, 100+150+50+130+270+750+50+70+500]
``````

This means it should stop once it hits 3 and add all values before that.

Right, that's what it's currently doing.

No, output from the code you suggested is

``````[100, 150]
[200, 1400]
[1470, 1970]
``````

However it is not what I want.

I edited the post with my desired output.

So `end = 100` rather than `end = 0` on the second line.

No that will mess with entire for loop below : it gives the following output when I have end = 100

``````[100, 250]
[300, 1500]
[1570, 2070]
``````

Those are the exact values you specified earlier.

``````[100, 100+150]

[100+150+50, 100+150+50+130+270+750+50]

[100+150+50+130+270+750+50+70, 100+150+50+130+270+750+50+70+500]
``````

These are the values I am trying to get in the output.

Presumably meaning you don't want the values actually added, but just separated by a plus. It'd be useful if you very explicitly stated if that's the case, since normally people would read `100+150` as meaning, "I want those two values to be summed such that 250 is the second element of the list".

Yes, I want those values to be summed. I had them like 100+150 so that people could understand what am I adding to get the final value.\

Sorry for not clarifying it earlier.

``````pos = [10, 100, 1000]

cigar = [[(0, 150), (3, 50), (0, 130), (3, 270), **(0, 750),(2,50),(1, 70), (0,500)**], [(0, 250), (3, 250), (0, 160), (3, 270), (0, 750)], [(0, 150), (3, 150), (0, 130), (3, 250), (0, 950)]]

def find (cigar, pos):
matches=list()
result=list()
end = 0
for i in cigar:
if i == 1:
continue
end = pos + i
item = [i, pos, end]
result.append(item)
pos=end

#print result
for i in result:
if i == 3:

matches.append([i, i])

return matches

for x, y in zip(cigar, pos):
#print x, y
res = find(x, y)
print res
``````

This gives me output however it stops the calculation for bold part (**). I want it to continue.