pythongrasshopper

store a matrix (or a 2D array)as values for each key in a dictionary


I have the following table coming from SQL:

b_num   loc max_neg_m   
10    0.89  -480050 
10    0.93  -144107 
10    0.96  -364381 
10    1     -699219 
100   0     -904713 
100   0.2   -772981 
100   0.41  -645617 
100   0.5   -563448 
100   0.67  -520747 
100   1     -218334 
1000  0     -112367 
1000  0.05  15976   

with a python code, I'm looking to store a 2D array of loc, max_neg_m for each unique b_num.

a=dict()

a.keys(b_num)

a.values=[loc,max_neg_m]

I appreciate any help.


Solution

  • Yes, storing 2D arrays as dictionary values is absolutely possible. Do this:

    dictionary = {}
    
    # Replace ``sql_query_result`` with the appropriate value.
    for b_num, loc, max_neg_m in sql_query_result:
        if b_num not in dictionary.keys():
            dictionary[b_num] = []
    
        dictionary[b_num].append([loc, max_neg_m])