比较列表并获取每个元素的匹配百分比

时间:2020-10-02 19:54:33

标签: python

我有两个python列表。

A= ['today is sunday', 'today is wednesday']
B= ['today is sunday', 'today is 
     Monday'....'today is Saturday']

 For elm in A:
     If elm in B:
        print ("not ok")
      else:
        print ("ok")

我想计算两个python列表之间的元素匹配百分比。

图像中显示了所需的匹配百分比计算:

enter image description here

2 个答案:

答案 0 :(得分:0)

可以是:

A= ['today is sunday', 'today is wednesday']
B= ['today is sunday', 'today is monday', 'today is Saturday']
match_percent = (len(set(B).intersection(set(A))))/len(B)*100
print(match_percent)

答案 1 :(得分:0)

我不确定您要计算的匹配百分比是多少,因此我以match_count / max(list_a_size, list_b_size)来计算。

def intersection(lst1, lst2): 
    lst3 = [value for value in lst1 if value in lst2] 
    return len(lst3)

def maximum(a, b): 
    if a >= b: 
        return a 
    else: 
        return b 

A= ['today is sunday', 'today is wednesday']
B= ['today is sunday', 'today is Monday', 'today is Saturday']

match_percent = intersection(A, B) / maximum(len(A),len(B))

print(match_percent)

输出:

0.3333333333333333