Python - 比较两个列表以查找计数

时间:2018-04-16 10:11:04

标签: python dictionary data-structures set

这是来自以下链接的扩展问题

Python - Find a tuple in list of lists

我一直在使用以下解决方案

# Your input data.
tuples = [(2,3), (3,6), (1,2)]
lists = [[1,2,3,4],[2,3,4,5],[2,3],[4,5,6]]

# Convert to sets just once, rather than repeatedly
# within the nested for-loops.
subsets = {t : set(t) for t in tuples}
mainsets = [set(xs) for xs in lists]

# Same as your algorithm, but written differently.
tallies = {
    tup : sum(s.issubset(m) for m in mainsets)
    for tup, s in subsets.items()
}

print(tallies)

它适用于给定的解决方案但是当我的lists size = 541909tuples size = 3363671需要花费很多时间时。它一直在运行30 minutes,我还没有得到输出。每个列表/元组中的元素将按升序排列,我准备更改这些元素的数据结构。什么是最快的方法来进行这项操作?

1 个答案:

答案 0 :(得分:2)

通过使用CREATE PROCEDURE <procedure name> IF (number of events> = 100) DELETE FROM events WHERE id IN (SELECT TOP 1 * id FROM table ORDER BY DueDate) 构建字典,我看到了一些性能提升:

collections.defaultdict