Multiple looping in python
1
0
Entering edit mode
7.0 years ago
KVC_bioinfo ▴ 600

Hello all,

I have the following code:

pos = 100
cigar = [(0, 50), (3, 50), (0, 100), (3, 200), (0, 50)]

result=list()

for i in cigar:

  end = pos + i[1]
  #print i[1]
  item = [i[0], pos, end]
  result.append(item)
  pos=end
matches=list()
for i in result:
  if i[0]<1:
    matches.append([i[1], i[2]])
print matches

This gives me the expected output. However, I want to modify this such that it should work for list of pos and cigar for example:

pos = [125, 130, 142]

cigar = [[(0, 150), (3, 50), (0, 130), (3, 270), (0, 750)], [(0, 250), (3, 250), (0, 160), (3, 270), (0, 750)], [(0, 150), (3, 150), (0, 130), (3, 250), (0, 950)]]

I am trying to use for loop to iterate through it. However, I am not able to multiloop it. Could someone help me here?

Thank you very much in advance.

python • 1.9k views
ADD COMMENT
0
Entering edit mode
    import pysam
    import sys

   pos = [125, 130, 142]

    cigar = [[(0, 150), (3, 50), (0, 130), (3, 270), (0, 750)], [(0, 250), (3, 250), (0, 160), (3, 270), (0, 750)], [(0, 150), (3, 150), (0, 130), (3, 250), (0, 950)]] 
    result=list()

    def find (cigar, pos):

        for i in cigar:
            end = pos + i[1]
            #print i[1]
            item = [i[0], pos, end]
            result.append(item)
            pos=end
        matches=list()
        for i in result:
            if i[0]<1:
                matches.append([i[1], i[2]])
        print matches

    for x, y in zip(cigar, pos):
        find(x, y)

I could run this using a function. However, in the out put:

[[125, 275], [325, 455], [725, 1475]]

[[125, 275], [325, 455], [725, 1475], [130, 380], [630, 790], [1060, 1810]]

[[125, 275], [325, 455], [725, 1475], [130, 380], [630, 790], [1060, 1810], [142, 292], [442, 572], [822, 1772]]

It is repeating the first list and then continuing. Could someone help me identifying why this is happing so?

ADD REPLY
1
Entering edit mode

You need to make sure that the list matches is only created once. Create it outside of the function.

pos = [125, 130, 142]

cigar = [[(0, 150), (3, 50), (0, 130), (3, 270), (0, 750)], [(0, 250), (3, 250), (0, 160), (3, 270), (0, 750)], [(0, 150), (3, 150), (0, 130), (3, 250), (0, 950)]]
result=list()
matches=list()
def find (cigar, pos):
    for i in cigar:
        end = pos + i[1]
        item = [i[0], pos, end]
        result.append(item)
        pos=end
    for i in result:
        if i[0]<1:
            matches.append([i[1], i[2]])
    return matches
for x, y in zip(cigar, pos):
    find(x, y)
print(matches)
ADD REPLY
0
Entering edit mode

Thank you very much. this worked perfectly.

ADD REPLY
1
Entering edit mode
7.0 years ago
Hussain Ather ▴ 990

How do you want to multiloop it? What have you tried?

ADD COMMENT
0
Entering edit mode
pos = [125, 130, 142]

cigar = [[(0, 150), (3, 50), (0, 130), (3, 270), (0, 750)], [(0, 250), (3, 250), (0, 160), (3, 270), (0, 750)], [(0, 150), (3, 150), (0, 130), (3, 250), (0, 950)]] 
result=list()
for i in cigar:
    for j,k in i:
        #print j
    for l in pos:
        #print l
        print i[1]
        end = j + i[1]
        item = [i[0], j, end]
        result.append(item)
        l=end
    matches=list()
for i in result:
    if i[0]<1:
        matches.append([i[1], i[2]])
print matches
ADD REPLY
0
Entering edit mode

This is what I have been trying.

ADD REPLY
2
Entering edit mode

You need a new indentation for each for loop. If you want to iterate over each value from cigar and pos, you need something like:

for i in cigar:
    for j,k in i:
        for l in pos:
ADD REPLY
0
Entering edit mode

Thank you. I get the following error.

Traceback (most recent call last):
  File "tmp1.py", line 13, in <module>
    end = l + i[1]
TypeError: unsupported operand type(s) for +: 'int' and 'tuple'

I think putting the entire code in a function and looping the function for input list can be helpful. But I am not sure how to do that.

ADD REPLY
2
Entering edit mode

Figure out what two things you want to connect with the + operator and figure out the way to call them properly. If you are calling two integers that you want summed, you need the appropriate way to call them. Right now, you are calling an integer and a tuple. To get the integer from a tuple, use another pair of square brackets at the end with the index inside. For example, if I have the tuple a = (1, 2) and I want the second index, I call it like a[1].

ADD REPLY

Login before adding your answer.

Traffic: 1670 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