How to flag end of loop over dictionary with two int's tuple as key - combination-like loop needed
Question: i have in memory a dictionary with the following pattern:

    
    
    value_refs[tuple([a,b])] = some float value
    

the dictionary is a pool of all the possible combinations of the values of
4000 references, previously computated (some millions). ex:

    
    
    ...
    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
    

The problem is that i don't know how to make a loop over the entire dictionary
keys in the same mode as 'combinations' do, using them as iterables, like:

    
    
    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...
    

I know that the above code doesn't work, because of bad syntax but i think
that is the best way to post the question. The unordered nature of the
dictionaries in python is (i think) a problem to that nested loops process the
2-tuple keys by an ordered way, like 1,2, 1,3 ... 1,4000 2,3 2,4 and so on.
How can i iterate over my dictionary in memory in an ordered way (by rank) and
extract both the 2-tuple key and the value that is the biggest for the second
value in the key versus the first tuple-value in the same key, and this for
all combinations? Thanks in advance.


Answer: How about a 2D-array with 4000*4000 elements? Takes less memory and is faster
then such a dict. Especially, if you have **all the possible combinations**.

Have a look at [Numpy](http://www.numpy.org/).

    
    
    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]
    

