1

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)]

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • why the tuple inside the list? Is that two indexes? First the index in distances, then the index in the sublist? – Kenny Ostrom Aug 09 '19 at 12:52
  • Thanks for valuable comments its working but its count index value from 0. i want in my result to count first index value from 1. any suggestions? – Sakshi Gupta Aug 10 '19 at 09:23

4 Answers4

1

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]
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

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]
Austin
  • 25,759
  • 4
  • 25
  • 48
0

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

raunak rathi
  • 95
  • 1
  • 9
0

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.

Kenny Ostrom
  • 5,639
  • 2
  • 21
  • 30