Reputation: 11
i have a nested list with some values and 1 list of minimum values in that each list of list. I want to find index value of that minimum value in list of list.
i have list of list like that-
distances=[[6.75, 0.75, 0.25, 2.25, 2.75, 1.75, 5.75, 2.75, 3.75, 7.75], [1.833333333333333, 4.166666666666667, 5.166666666666667, 7.166666666666667, 2.166666666666667, 3.166666666666667, 0.833333333333333, 2.166666666666667, 1.166666666666667, 2.833333333333333]]
list of minimum values in above list-
minimum_distance= [0.25, 0.833333333333333]
i have tried below code- but
def find(c):
for i, distance in enumerate(distances):
try:
j = distance .index(c)
except ValueError:
continue
yield i, j
matches = [match for match in find(k for k in minimum_distance)]
print(matches)
resultant list is empty, i want like [(1,6)]
Upvotes: 1
Views: 103
Reputation: 5871
I'm trying to figure out why the tuple? And why is the 2 a 1? Did you mean [(0,2), (1,6)] so that you have the index in list of lists paired with the index of the minimum in each sublist? Looking at your find function, I have a guess.
If you want the index in distances as well, so my first thought was to enumerate the zip, but unpacking that is problematic. Instead we use itertools.count and zip all three together.
see Enumerate two python lists simultaneously?
from itertools import count
distances=[[6.75, 0.75, 0.25, 2.25, 2.75, 1.75, 5.75, 2.75, 3.75, 7.75], [1.833333333333333, 4.166666666666667, 5.166666666666667, 7.166666666666667, 2.166666666666667, 3.166666666666667, 0.833333333333333, 2.166666666666667, 1.166666666666667, 2.833333333333333]]
minimum_distance= [0.25, 0.833333333333333]
print([(i, distance_list.index(minimum))
for i, distance_list, minimum
in (zip(count(), distances, minimum_distance))])
[(0, 2), (1, 6)]
This is similar to what your find function is doing, but generally you want a generator OR a list comprehension.
Upvotes: 0
Reputation: 95
I modified your code:
def find1(c):
j = c .index(min(c))
return j
matches=[]
for match in distances:
matches.append(find1(match))
print(matches)
[2,6]
In your case you are calling function in list which is will convert to generator
Upvotes: 0
Reputation: 26039
You can use zip
if you already have minimum_distance
calculated:
[x.index(y) for x, y in zip(distances, minimum_distance)]
Example:
distances=[[6.75, 0.75, 0.25, 2.25, 2.75, 1.75, 5.75, 2.75, 3.75, 7.75], [1.833333333333333, 4.166666666666667, 5.166666666666667, 7.166666666666667, 2.166666666666667, 3.166666666666667, 0.833333333333333, 2.166666666666667, 1.166666666666667, 2.833333333333333]]
minimum_distance= [0.25, 0.833333333333333]
print([x.index(y) for x, y in zip(distances, minimum_distance)])
# [2, 6]
Upvotes: 0
Reputation: 82755
This is one approach using list.index
with min
.
Ex:
distances=[[6.75, 0.75, 0.25, 2.25, 2.75, 1.75, 5.75, 2.75, 3.75, 7.75], [1.833333333333333, 4.166666666666667, 5.166666666666667, 7.166666666666667, 2.166666666666667, 3.166666666666667, 0.833333333333333, 2.166666666666667, 1.166666666666667, 2.833333333333333]]
matches = [i.index(min(i)) for i in distances ]
print(matches)
Output:
[2, 6]
Upvotes: 1