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

我有两个 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 列表之间元素匹配的百分比。


所需的匹配百分比计算如图所示:

https://img1.mukewang.com/651e79110001980406551164.jpg

料青山看我应如是
浏览 72回答 2
2回答

月关宝盒

有可能 :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)*100print(match_percent)

互换的青春

我不确定您想要计算的确切匹配百分比,因此我冒昧地计算为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
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python