Difficulty in python while loop
1
0
Entering edit mode
6.9 years ago
KVC_bioinfo ▴ 600

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] == 0:
        if i == 3:
            break
        end = pos + i[1]
        item = [i[0], pos, end]
        #print item
        result.append(item)
        pos=end

matches=list()
for i in result:
  if i[0]<1:
    matches.append([i[1], i[2]])
#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.

python whileloop • 3.1k views
ADD COMMENT
1
Entering edit mode

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.

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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.

ADD REPLY
0
Entering edit mode

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

What do you get?

ADD REPLY
2
Entering edit mode
6.9 years ago

i == 3 can never be true, because i is a tuple. You'll want to test i[0] == 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)
ADD COMMENT
0
Entering edit mode
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.

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

No, output from the code you suggested is

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

However it is not what I want.

ADD REPLY
0
Entering edit mode

I edited the post with my desired output.

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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]
ADD REPLY
0
Entering edit mode

Those are the exact values you specified earlier.

ADD REPLY
0
Entering edit mode
[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.

ADD REPLY
0
Entering edit mode

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".

ADD REPLY
0
Entering edit mode

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.

ADD REPLY
0
Entering edit mode
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[0] == 1:
            continue    
        end = pos + i[1]
        item = [i[0], pos, end]
        result.append(item)
        pos=end

    #print result           
    for i in result:
        if i[0] == 3:

            matches.append([i[1], i[2]])

    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.

ADD REPLY
0
Entering edit mode

Could anyone help me to figure this out?

ADD REPLY

Login before adding your answer.

Traffic: 2639 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6