list去重及求兩個list中元素的重複率

2021-08-21 11:45:58 字數 653 閱讀 9299

假設有a,b兩個list;求兩個list中相同元素在其中乙個list中的佔比。

思路:1.分別用set()去除兩個list中個字重複的元素

2.將兩個list相加構成第三個listc

3.a,b各自去重後元素個數的和減去c中去重後元素的個數就是連個list重複的元素

a = ['a','b','c','ab','df','c']

b = ['a','b','df','h','op','sd','h']

a = list(set(a))# 去重 len(a)=5

b = list(set(b))# 去重 len(b)=6

unique_len=len(set(a+b)) # 8

total_len=len(a)+len(b) # 11

repeat_len=total_len-unique_len # 3 a b df

repeat_rate=repeat_len/len(a) # 3 / 5 = 0.6

print(repeat_rate)# 0.6

兩個list比較,去重複

現有list1,list2,兩個list內容做比較,剔除list1中重複的元素,是乙個比較簡單的問題,直接貼 int size list1.size for int i 0 i list2.size i 不過上述方法效率都不高,尋找更好的方法中!當然,我們可以不用自己比較,直接使用list提供的方法...

list中的字典去重

list中的字典 格式 list dict 如上,list dict中有三個字典,但是是重複的,這裡需要去重,保留乙個不重複的dict即可 def list dict duplicate removal list dict list中dict重複的話,去重 run function lambda x...

兩個list的差集 交集 去重並集計算

獲取兩個arraylist的差集 交集 去重並集 資料量大小不限制 此 是copy同事的 不知道是不是原創 private static void getlist 獲取交集 collectionlist receivecollectionlist firstarraylist,secondarray...