首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >查找公共项数量的快速方法python

查找公共项数量的快速方法python
EN

Stack Overflow用户
提问于 2019-05-27 19:14:07
回答 2查看 123关注 0票数 0

我有一个包含145000个物料(物料清单)的大型数据,我想检查两个物料清单之间共享物料的百分比。

两个for循环或其他方法总是在相似的时间段内运行。

完成此操作的最快方法是什么?

First&secondbill是包含组件的列表:

代码语言:javascript
运行
复制
for FKid in FirstBill: 
      for SKid in SecondBill:
            CommonChild = (CommonChild + 1) if FKid == SKid else CommonChild
    return CommonChilds / len(FirstBill)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-27 19:17:46

使用一个集合的最佳选择

代码语言:javascript
运行
复制
# 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))
票数 0
EN

Stack Overflow用户

发布于 2019-05-27 19:23:14

假设账单中的in是唯一的,一个更简单的答案是:

代码语言:javascript
运行
复制
percentage = sum([1 for fkid in FirstBill if fkid in SecondBill]) / len(FirstBill) * 100

代码语言:javascript
运行
复制
percentage = len(set(FirstBill).intersection(set(SecondBill))) / len(FirstBill) * 100
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56324990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档