4

I have a dictionary like

A = {1:{1:50,2:60,5:90},2:{7:55,10:102},4:{10:100,12:40}}

How can i access the inner indexes of the dictionary i.e the keys 1,2,5 or the keys 10 and 12 I mean i want access these elements to store them in specific places of an array, but while using a loop, since some keys are absent, there is some kind of an error.(when i tried using A[i][j] in my nested loops). How to tackle this unevenness of keyss

karthikr
  • 97,368
  • 26
  • 197
  • 188
Abhishek Verma
  • 73
  • 1
  • 1
  • 5
  • possible duplicate of [Access python nested dictionary items via a list of keys](http://stackoverflow.com/questions/14692690/access-python-nested-dictionary-items-via-a-list-of-keys) – karthikr Sep 14 '14 at 18:08
  • Are you trying to convert a sparse matrix into a nonsparse one? – Joel Cornett Sep 15 '14 at 03:35

6 Answers6

4

If you want to access value for specific keys,you can do that using A[outer_key][inner_key]

for example

>>> A[1][1]
50
>>> A[2][10]
102

If you want to iterate over all the keys , you can use for as shown below.

>>> A
{1: {1: 50, 2: 60, 5: 90}, 2: {10: 102, 7: 55}, 4: {10: 100, 12: 40}}
>>> for outer_key in A:
...     print 'Outer Key = ',outer_key
...     for inner_key in A[outer_key]:
...             print '%d,%d' % (inner_key,A[outer_key][inner_key])
... 
Outer Key =  1
1,50
2,60
5,90
Outer Key =  2
10,102
7,55
Outer Key =  4
10,100
12,40
g4ur4v
  • 3,210
  • 5
  • 32
  • 57
1

You should use the built in iterators to accomplish this task.

A = {1:{1:50,2:60,5:90},2:{7:55,10:102},4:{10:100,12:40}}

myArray = list()

for innerDict in A.values():
    for value in innerDict.values():
        myArray.append(value)

print (myArray)
Dan
  • 1,874
  • 1
  • 16
  • 21
1

This will iterate through the structure and fill the values into a numpy array:

A = {1:{1:50,2:60,5:90},2:{7:55,10:102},4:{10:100,12:40}}
B = numpy.zeros((4, 12))

for i, row in A.items():
    for j, value in row.items():
        B[i-1, j-1] = value
chthonicdaemon
  • 19,180
  • 2
  • 52
  • 66
0
A = {1:{1:50,2:60,5:90},2:{7:55,10:102},4:{10:100,12:40}}

values =[ x for y in A.itervalues() for x in y.itervalues()]
[50, 60, 90, 102, 55, 100, 40]

If you want the key and value pairings:

key_values =[ x for y in A.itervalues() for x in y.items()]
print key_values

[(1, 50), (2, 60), (5, 90), (10, 102), (7, 55), (10, 100), (12, 40)]
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

Inorder to get sub-components (rj is a JSON object)

items=rj.items()
si = sorted(items, key=operator.itemgetter(0))

for i in si:
    t=-1
    print i[0]
    for k in i[1]:
        t+=1
        print '   ',i[1].keys()[t],':',i[1].get(k)

print len(rj)
Bircan
  • 29
  • 3
0

You can use get function on dictionary which returns None when key is not found in dictionary. So, if all the common keys are saved in some set, then iteration through it can be done:

A = {1:{1:50,2:60,5:90},2:{7:55,10:102},4:{10:100,12:40}}

# storing all keys in set not to save repeated keys
keys_list = {key for inner_dict in A.values() for key in list(inner_dict)}

# for each outer key
for i in A.keys():
    print(A[i]) # printing each key to check

    for j in keys_list: # looping through inner dictionary
        if A[i].get(j) is not None: # if the key is found then it is not None
            print ('Outer Key: ', i, " Inner Key: ", j, " Value: ",  A[i][j]) # now you can use A[i][j]

    print('--------------------------------------------------')

Output:

{1: 50, 2: 60, 5: 90}
Outer Key:  1  Inner Key:  1  Value:  50
Outer Key:  1  Inner Key:  2  Value:  60
Outer Key:  1  Inner Key:  5  Value:  90
--------------------------------------------------
{10: 102, 7: 55}
Outer Key:  2  Inner Key:  7  Value:  55
Outer Key:  2  Inner Key:  10  Value:  102
--------------------------------------------------
{10: 100, 12: 40}
Outer Key:  4  Inner Key:  10  Value:  100
Outer Key:  4  Inner Key:  12  Value:  40
--------------------------------------------------
niraj
  • 17,498
  • 4
  • 33
  • 48