python中的快速数据比较

sbe*_*ria 3 python comparison dictionary list

我想以2种不同长度的字典形式比较大量数据.(编辑)

post = {0: [0.96180319786071777, 0.37529754638671875], 
        10: [0.20612385869026184, 0.17849941551685333],
        20: [0.20612400770187378, 0.17510984838008881],...}

pre = {0: [0.96180319786071777, 0.37529754638671875],
       1: [0.20612385869026184, 0.17849941551685333],
       2: [0.20612400770187378, 0.17510984838008881],
       5065: [0.80861318111419678, 0.76381617784500122],...}
Run Code Online (Sandbox Code Playgroud)

我们需要得到的答案是5065:[0.80861318111419678,0.76381617784500122].这是基于我们只是比较价值而不是指数的事实.

我只使用这个键值对来记住数据序列.如果需要,可以用列表/集替换数据类型.我需要找出关键:与字典不相同的元素的值(索引和值)对.

我使用的代码非常简单..

new = {}
found = []

for i in range(0, len(post)): 
    found= []
    for j in range(0, len(pre)): 
        if post[i] not in pre.values():
            if post[i] not in new:
                new[i] = post[i]
                found.append(j)             
                break
    if found:
        for f in found: pre.pop(f)
Run Code Online (Sandbox Code Playgroud)

new {}包含我需要的元素.我面临的问题是这个过程太慢了.处理有时需要一个多小时.有时数据可能会大得多.我需要它更快.

有没有一种有效的方法来做我想要实现的目标?除非绝对必要,否则除了那些与python 2.5(64位)捆绑在一起的外部软件包之外我不想依赖外部软件包.

谢谢你们.

Amb*_*ber 5

这基本上是为什么set设计的(计算项目集的差异).唯一的问题是你set需要的东西是可以清洗的,而list不是.但是,tuples是,所以如果转换为那个,你可以把它们放到一个集合中:

post_set = set(tuple(x) for x in post.itervalues())
pre_set = set(tuple(x) for x in pre.itervalues())

items_in_only_one_set = post_set ^ pre_set
Run Code Online (Sandbox Code Playgroud)

有关sets的更多信息:http://docs.python.org/library/stdtypes.html#set

要在计算差异后获取原始索引,您可能需要生成反向查找表:

post_indices = dict((tuple(v),k) for k,v in post.iteritems())
pre_indices = dict((tuple(v),k) for k,v in pre.iteritems())
Run Code Online (Sandbox Code Playgroud)

然后你可以只取一个给定的元组并通过字典查找它的索引:

index = post_indices.get(a_tuple, pre_indices.get(a_tuple))
Run Code Online (Sandbox Code Playgroud)