Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a two-dimensional list. I would like to get the two elements from list_j, i.e. the two lists that have the highest agreement with list_g. I've tried something, but how do I get the two lists that match the most?

list_j = [[100,2,3], [4,98,99], [5,99,98]]

list_g = [100,99,98]

import difflib
list_ratio = []
for element_g in list_j:
  sm=difflib.SequenceMatcher(None,element_g,list_g)
  list_ratio.append(sm.ratio())

print(list_ratio)

[OUT] [0.3333333333333333, 0.3333333333333333, 0.6666666666666666]

In this example, the last element then 1 or 2 has the most similarity. How could I get them?

question from:https://stackoverflow.com/questions/65644038/get-only-the-two-lists-with-the-most-similarity

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
133 views
Welcome To Ask or Share your Answers For Others

1 Answer

A common way to determine similarity for real number valued lists of the same length is with the RMSE metric. Python's math library allows us to calculate this metric if we iterate over the lists.

import math
list_j = [[100,2,3], [4,98,99], [5,99,98]]
list_g = [100,99,98]

The following function will return a similarity score based on the RMSE, along with the index of that list within list_j, as a two-dimensional list.

def similarity(list_j, list_g):
    dists = [list() for i in range(len(list_j))]
    for i_j,j in enumerate(list_j):
        for i in range(len(j)):
            dist = (j[i] - list_g[i])**2
            dists[i_j].append(dist)
        dists[i_j] = [math.sqrt(sum(dists[i_j])), i_j]
    return dists

Given that we want to find the two lists in list_j that are the most similar, the following function returns those lists in the order of their similarity.

def similar_two(list_j, dists):
    list_js = sorted(dists)
    list_j_1 = list_js[0][1]
    list_j_2 = list_js[1][1]
    return list_j[list_j_1], list_j[list_j_2]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...