[英]Fast way to find number of common items python
我有145000件物品(物料清單)的大量數據,我想檢查兩個物料清單之間共享物品的百分比。
兩個for循環或其他方法總是在相似的時間段內運行。
最快的方法是什么?
第一個和第二個是包含其中組件的列表:
for FKid in FirstBill:
for SKid in SecondBill:
CommonChild = (CommonChild + 1) if FKid == SKid else CommonChild
return CommonChilds / len(FirstBill)
有點使用一套最佳
# Python program to illustrate the intersection
# of two lists in most simple way
def intersection(lst1, lst2):
temp = set(lst2)
lst3 = [value for value in lst1 if value in temp ]
return lst3
# Driver Code
lst1 = [4, 9, 1, 17, 11, 26, 28, 54, 69]
lst2 = [9, 9, 74, 21, 45, 11, 63, 28, 26]
#print(intersection(lst1, lst2))
quantity = len(intersection(lst1, lst2))
假設賬單中的ID是唯一的,更簡單的答案是:
percentage = sum([1 for fkid in FirstBill if fkid in SecondBill]) / len(FirstBill) * 100
要么
percentage = len(set(FirstBill).intersection(set(SecondBill))) / len(FirstBill) * 100
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.