出现列表列表中的元素

时间:2016-11-08 07:48:36

标签: python list tuples frequency

假设我有一个列表列表

Select 
       cast((DATEDIFF(m, DOB, GETDATE())/12) as varchar) + ' Y & ' + 
       cast((DATEDIFF(m, DOB, GETDATE())%12) as varchar) + ' M & ' +
       cast((DATEDIFF(D, DOB, GETDATE())%12) as varchar) + ' d' 
from @T
WHERE DATEDIFF(m, DOB, GETDATE())/12 < 79 -- we need only people how are not already 79 years old
    AND DATEDIFF(m, DATEADD(DAY,-45, DOB), GETDATE())/12 >= 79 -- we are making the DOB 45 days samller

我有元组列表

 record = [['g1','g2','g3'],['g2','g4'],['g1','g3','g5'],['g2','g3','g5'],['g1','g4']]

现在记录中出现list1 = [('g1','g2'),('g1','g3'),('g1','g4'),('g1','g5'),('g2','g3'),('g2','g4'),('g2','g5'),('g3','g4'),('g3','g5'),('g4','g5')] 次? 解决方案应为1,因为('g1','g2')仅存在('g1','g2')

我可以将元组列表更改为列表列表。有没有简单的方法而不是蛮力?因为我的清单列表可能包含1000k物品

2 个答案:

答案 0 :(得分:1)

它不漂亮,但它有效:

uniform_int_distribution

编辑:
record = [['g1','g2','g3'],['g2','g4'],['g1','g3','g5'],['g2','g3','g5'],['g1','g4']] pattern = [('g1','g2'),('g1','g3'),('g1','g4'),('g1','g5'),('g2','g3'),('g2','g4'),('g2','g5'),('g3','g4'),('g3','g5'),('g4','g5')] res = {} for p in pattern: res[str(p)] = 0 for r in record: if set(p).issubset(set(r)): res[str(p)] += 1 print(res) 件物品? (那么这不会起作用......)

答案 1 :(得分:0)

将列表列表中的项g1, g2, ...视为无向图的顶点。浏览列表列表并构建图表。每次g1g2出现在同一子列表中时,g1 <-> g2的权重会增加1。然后,您要查找的数字是元组元素上边缘的重量。

这假定元组总是有两个元素。如果元组具有任意大小,除了子列表是任意的之外,那么这个问题减少到找到多个子图同构,每个子图同构都是NP完全的。请参阅:https://stackoverflow.com/a/5279581/1749870