我有一个包含145000个物料(物料清单)的大型数据,我想检查两个物料清单之间共享物料的百分比。
两个for循环或其他方法总是在相似的时间段内运行。
完成此操作的最快方法是什么?
First&secondbill是包含组件的列表:
for FKid in FirstBill:
for SKid in SecondBill:
CommonChild = (CommonChild + 1) if FKid == SKid else CommonChild
return CommonChilds / len(FirstBill)
发布于 2019-05-27 19:17:46
使用一个集合的最佳选择
# 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))
发布于 2019-05-27 19:23:14
假设账单中的in是唯一的,一个更简单的答案是:
percentage = sum([1 for fkid in FirstBill if fkid in SecondBill]) / len(FirstBill) * 100
或
percentage = len(set(FirstBill).intersection(set(SecondBill))) / len(FirstBill) * 100
https://stackoverflow.com/questions/56324990
复制相似问题