[英]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.