为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

料青山看我应如是 2023-10-05 16:47:57
我有两个 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 列表之间元素匹配的百分比。所需的匹配百分比计算如图所示:
查看完整描述

2 回答

?
月关宝盒

TA贡献1772条经验 获得超5个赞

有可能 :


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)


查看完整回答
反对 回复 2023-10-05
?
互换的青春

TA贡献1797条经验 获得超6个赞

我不确定您想要计算的确切匹配百分比,因此我冒昧地计算为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


查看完整回答
反对 回复 2023-10-05
  • 2 回答
  • 0 关注
  • 70 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信