[英]Algorithm to add new, update existing and remove missing data
I have done lots of searching, but been unable to find a satisfactory answer to the most efficient approach to achieve the following.我进行了大量搜索,但无法找到满意的答案来实现以下目标的最有效方法。
Say my App contains a list of Products.假设我的应用程序包含一个产品列表。 At the end of every day an external service is called that returns another list of Products from a master data source.在每天结束时,调用外部服务从主数据源返回另一个产品列表。
At the moment, I do a loop on each list, looping through the other list for each Product:目前,我在每个列表上循环,为每个产品循环另一个列表:
I'm wondering if there is a more efficient method to achieve this?我想知道是否有更有效的方法来实现这一目标? Or any algorithms or patterns that are relevant here?或者任何与此处相关的算法或模式?
In each case the Products are represented by objects in a Python list.在每种情况下,产品都由 Python 列表中的对象表示。
First of all I'd suggest to use dict
s with the Product code (or name or whatever) as key and the Product object
as value.首先,我建议使用dict
以 Product 代码(或名称或其他)作为键,将 Product object
作为值。 This should make your loops faster by at least a 100x factor on a thousand entries.这应该使您的循环速度在一千个条目上至少提高 100 倍。
Then especially for the second search it may be worth exploring the possibility of converting the keys of the first dict
to a set
and looping on the difference as in然后特别是对于第二次搜索,可能值得探索将第一个dict
的键转换为set
并循环差异的可能性,如
for i in set(appDict.keys()).difference(masterDict.keys()):
##update unavailable Product data
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.