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
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.
1 answer
How do you want to multiloop it? What have you tried?
This is what I have been trying.
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:
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.
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].
Log in to answer this question.
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?
You need to make sure that the list
matchesis only created once. Create it outside of the function.Thank you very much. this worked perfectly.