I have a list of tuples:
list = [(1,-1),(1,1),(2,0),(3,-9),(3,9),(4,-10),(4,-8),(4,8),(4,10),(5,-25),(5,25),(5,-9),(5,9)]
Here is what I want to do:
- For every number in the first place, i.e. for 1, 2, 3, 4, and 5, find the minimum of abs(second value). If there are multiple minima, find all of them. For instance, for '1' in the first place both (1,-1) and (1,1) qualify because abs(-1)=abs(1)
- Create another list with 1s in corresponding places where the tuples in the list qualify and 0 where they do not. The answer is
[1,1,1,1,1,0,1,1,0,0,0,1,1]
Here is my code:
result=[]
temp_first=list[0][0]
temp_second=abs(list[0][1])
result.append(1)
for element in list[1:]:
if element[0]==temp_first:
if abs(element[1])<temp_second:
result[-1]=0
result.append(1)
elif abs(element[1])>temp_second:
result.append(0)
else:
result.append(1)
else:
result.append(1)
temp_first=element[0]
temp_second=abs(element[1])
It gives me [1,1,1,1,1,0,1,1,0,1,0,1,1] which is incorrect
Any help will be appreciated