我有一个应该插入数据库的dict inserts
列表。因为有些元素已经在dbdb_entry_ids
中了,所以我想过滤掉它们。现在代码非常简单:
[d for d in inserts if d["entry_id"] not in db_entry_ids]
与
len(inserts) = 35000
len(db_entry_ids) =280000
Python需要几分钟的时间。我希望有一些简单的方法来提高效率。你知道吗
PS:d["entry_id"]
是字符串,db_entry_ids
是字符串列表。所以id
可能有点误导。你知道吗
Tags:
将
db_entry_ids
转换为一个集合。集合中的not in
运算要快得多。你知道吗请参见以下参考:https://wiki.python.org/moin/TimeComplexity
相关问题 更多 >
编程相关推荐