亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

比較列表并獲取每個元素的匹配百分比

比較列表并獲取每個元素的匹配百分比

料青山看我應如是 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 關注
  • 153 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號