【问题标题】:Efficient and fast Data Storage and Processing in Python of multiple dictionaries and dictionary of lists ad intersection of two dictionaries of lists在 Python 中高效、快速地存储和处理多个字典和列表字典以及两个列表字典的交集
【发布时间】:2018-07-23 14:33:27
【问题描述】:

我有一本表格字典 示例:-

 all_ways = {key1:[list1], key2:[list2], ... keyN[listN]}

我只想找到ith 列表中的那些元素,这样它至少是另一个列表的一部分,比如jth 列表(i!=j),然后对于所有键,只存储满足上述条件的那些元素.

除了使用多个循环之外,还有什么好的方法吗?

例子:-

key1: [1,2,3,4,5,100,111,123] 
key2: [1,2,3,13,15,17] 
key3:[100,110,111,1000] 

答案:

key1:[1,2,3,100,111] 
key2:[1,2,3]
key3:[100,111]

【问题讨论】:

  • 本能地说,如果没有循环,这可能很难。每个列表大约有多少个字典项/项?
  • 70,000 个键,每个键的列表中平均有 50 个元素。
  • 可能,这可以通过字典理解+单循环有效地处理。但我会等着看是否有人提供替代方案。
  • 举个例子就好了。
  • 示例:key1: [1,2,3,4,5,100,111,123] key2: [1,2,3,13,15,17] key3:[100,110,111,1000] 答案:key1:[ 1,2,3,100,111] key2:[1,2,3] key3:[100,111]

标签: python python-3.x algorithm dictionary data-structures


【解决方案1】:

相对于列表元素的总数,您可以在线性时间(无嵌套循环)内实现这一点:

from collections import Counter
from itertools import chain

d = {'key1': [1,2,3,4,5,100,111,123],
     'key2': [1,2,3,13,15,17],
     'key3': [100,110,111,1000]}

# histogramm over the chained lists
c = Counter(chain(*d.values()))  
# c = Counter(chain(*map(set, d.values())))  # if there are duplicates in the lists

# for every key k in d, pick only those elements x from their value lists v
# that occur more than once overall (thus in multiple lists)
result = {k: [x for x in v if c[x] > 1] for k, v in d.items()}
# {'key1': [1, 2, 3, 100, 111], 
#  'key2': [1, 2, 3], 
#  'key3': [100, 111]}

collections.Counter 保存元素出现在每个唯一列表元素中的列表计数,同时忽略单个列表中的重复项(通过set)。然后嵌套推导只选择计数大于1 的元素,从而保证它们出现在多个列表中。

【讨论】:

  • 好的,这很有帮助。如果你能告诉我算法是如何工作的,而不仅仅是实现,那就太好了。
  • 一个明显的性能点。一旦注册了 2 个项目,Counter 就不会停止计数。所以它做的工作比它应该做的要多。
  • @DivyangVashi 我添加了一些解释。
  • @jp_data_analysis 确实如此,但是您需要以任何一种方式查看每个元素,以便知道您是否已经看过两次。实施您自己的检查几乎不会击败优化后的计数器。
  • @schwobaseggl,好吧,您每次都在保存更新字典值。在这个(相对较小的)数据集中它很好。只是一个观察。我确实赞成您的回答,这是最干净的解决方案。
【解决方案2】:

以下是其他几种方法,它们在实际数据集上进行了基准测试。此答案假定列表中没有重复项,并且输出的顺序(每个键的重复项)并不重要。

import numpy as np
from itertools import groupby, chain
from collections import Counter

d = {'key'+str(i): list(np.random.randint(0, 1000000, 50)) for i in range(1, 50000)}

def return_dups_jp1(d):
    def get_dups(a):
        seen = set()
        dup = set()
        for x in a:
            if x not in seen:
                seen.add(x)
            else:
                dup.add(x)
        return dup

    def apply_dups(d, dups):
        return {k: list(set(v) & dups) for k, v in d.items()}

    return apply_dups(d, get_dups(chain.from_iterable(d.values())))

def return_dups_jp2(d):
    b = sorted(chain.from_iterable(d.values()))
    dups = {group[0] for group in groupby(b) if len(list(group[1])) > 1}
    return {k: list(set(v) & dups) for k, v in d.items()}

def return_dups_sch(d):
    c = Counter(chain(*d.values()))
    return {k: [x for x in v if c[x] > 1] for k, v in d.items()}

基准测试:

x = return_dups_jp1(d)
y = return_dups_jp2(d)
z = return_dups_sch(d)

assert all(set(x['key'+str(i)]) == set(y['key'+str(i)]) for i in range(1, 50000))
assert all(set(y['key'+str(i)]) == set(z['key'+str(i)]) for i in range(1, 50000))

%timeit return_dups_jp1(d)
# 1 loop, best of 3: 2.56 s per loop

%timeit return_dups_jp2(d)
# 1 loop, best of 3: 5.12 s per loop

%timeit return_dups_sch(d)
# 1 loop, best of 3: 2.66 s per loop

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-10
    • 2016-02-06
    • 2019-07-15
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    相关资源
    最近更新 更多