首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用两个int元组将字典循环结束标记为所需的类似键组合的循环

如何使用两个int元组将字典循环结束标记为所需的类似键组合的循环
EN

Stack Overflow用户
提问于 2013-04-30 20:34:16
回答 2查看 185关注 0票数 0

我在内存中有一本字典,它的模式如下:

代码语言:javascript
复制
value_refs[tuple([a,b])] = some float value

字典是之前计算过的4000个引用的值的所有可能组合的池(几百万)。例如:

代码语言:javascript
复制
...
value_refs[1,4] =0,76543
value_refs[1,5] =0,89734 #i want this value, since it is the bigger of all the second ref's,
                         # related with the ref. 1 (first tuple in the key)
...
value_refs[1,4000] =0,77543
...
...
value_refs[4000,30] =0,76543
value_refs[4000,31] =1,89734 # I want this value, since it is the bigger of all the second
                             # references, related with the ref. 4000 (first tuple in the key)
value_refs[4000,32] =0,77543

问题是,我不知道如何像“组合”一样循环遍历整个字典中的键,将它们用作迭代,比如:

代码语言:javascript
复制
asymptote=0
cache=[]
pool_chain={}

for c in value_refs.keys()[c][0]: # [0] because i need the first tuple value of the key, by rank
    for d in value_refs.keys()[d][1]: # [1] because i need a loop over the range of all the second
                                    #tuple values in the dict pool, versus the outer loop
        while True:
            try:
                if value_refs[c,d] > asymptote:
                    cache=[c,d]
                    asymptote=value_refs[c,d]
            except KeyError:
                pass
            except StopIteration:
                pool_chain[cache]=asymptote
                asymptote=0
        #and now c would advance by an ordered rank intil the number 4000...

我知道上面的代码不能工作,因为语法不好,但我认为这是发布问题的最好方法。python中字典的无序性(我认为)是一个问题,因为嵌套循环以有序的方式处理2元组键,如1,2,1,3... 1,4000 2,32,4等等。我如何在内存中以有序的方式(按排名)迭代我的字典,并提取2元组关键字和值,该值是关键字中第二个值与同一关键字中第一个元组值的最大值,以及所有组合的最大值?提前谢谢。

EN

回答 2

Stack Overflow用户

发布于 2013-04-30 20:39:05

有4000*4000个元素的2D数组怎么样?比这样的字典占用更少的内存,速度更快。尤其是,如果你有所有可能的组合

看看Numpy吧。

代码语言:javascript
复制
import numpy as np

arr = np.empty((4000,4000))
for i, a in enumerate([...]):
    for j, b in enumerate([...]):
        arr[i, j] = ...

...

for i in arr.shape[0]:
    for j in arr.shape[1]:
        ... arr[i, j]
票数 0
EN

Stack Overflow用户

发布于 2013-04-30 20:42:19

我认为您要做的是收集每个唯一密钥对的第一个值的最大值。

以下是一种方法:

代码语言:javascript
复制
from collections import defaultdict

all_values = defaultdict(list)

keys = value_refs.keys()

for k in keys:
   all_values[k[0]].append(value_ref[k])

for k,v in all_values.iteritems():
   print i,max(v)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16300040

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档