1

I need a little help with a small task.

I have a dictionary with a tuple of integers (e.g. (1,1), (0,2), etc.)

I want to be able to put this in a matrix that looks like this...

(0,0) | (0,1)  | (0,n)
______|________|_______
(1,0) | (1,1)  | (1,n)
______|________|_______
(m,0) | (m,1)  | (m,n)

Here is a code that I thought would help me append each value on the matrix with the keys in my dictionary.

import numpy as np

rows = 3
columns = 4

my_dict = {}
for i in range(rows):
    for j in range(columns):
        my_dict[i,j] = []

A = np.arange(rows*columns).reshape([rows,columns])
for keys in my_dict.keys():
    for i in range(rows):
        for j in range(columns):
            A[i,j] = my_dict[keys]

I get the following error. ValueError: setting an array element with a sequence.

I believe I am getting this error because it is assigning the key's value, respectfully, which is an empty list. This isn't my intention. I want to associate each element in the matrix with keys associated with my dictionary.

Edit: The desired output should be the following...

(0,0) | (0,1)  | (0,2) | (0,3)  |
______|________|_______|________|
(1,0) | (1,1)  | (1,2) | (1,3)  | = A
______|________|_______|________|
(2,0) | (2,1)  | (2,2) | (2,3)  |
______|________|_______|________|
uBoscuBo
  • 81
  • 8
  • It's hard to understand why you would want to do this; keys identical to the index values themselves? `A[i,j] = (i,j)`? – anon01 Jan 14 '21 at 04:46
  • This thread already gives answer, and could be helpful : https://stackoverflow.com/questions/4674473/valueerror-setting-an-array-element-with-a-sequence – Yang Liu Jan 14 '21 at 06:11

2 Answers2

1

I'm not sure on your intention but it's just a wrong type which A represented an array of integer but your dictionary value giving a list type. So give its type to list would solve your error basically, but I am also hard to understand what practice in this case

np.arange(rows*columns, dtype = list).reshape([rows,columns])

Full

import numpy as np

rows = 3
columns = 4

my_dict = {}
for i in range(rows):
    for j in range(columns):
        my_dict[i,j] = []

A = np.arange(rows*columns, dtype = list).reshape([rows,columns])
for keys in my_dict.keys():
    for i in range(rows):
        for j in range(columns):
            A[i,j] = my_dict[keys]

Your A

enter image description here

---- Update from new editing issue

import numpy as np

rows = 3
columns = 4

my_dict = {}
for i in range(rows):
    for j in range(columns):
        my_dict[i,j] = []

A = np.arange(rows*columns, dtype = list).reshape([rows,columns])
for keys in my_dict.keys():
    A[keys[0], keys[1]] = keys

Result of A:

enter image description here

Tấn Nguyên
  • 1,607
  • 4
  • 15
  • 25
  • 1
    `dtype=object`. Anything that isn't a numpy numeric dtype (or string) is object. – hpaulj Jan 14 '21 at 06:20
  • I guess I wasn't clear. I am trying to append each integer in the matrix `A` with the key in my dictionary. The desired output should look like the one in my post. I didn't know `A` represents an array. I just assign some random variable. – uBoscuBo Jan 14 '21 at 07:11
  • @uBoscuBo I have updated, check and feedback if it's you desired output. – Tấn Nguyên Jan 14 '21 at 07:52
  • Thank you, I do appreciate it! – uBoscuBo Jan 14 '21 at 15:41
0

You could skip the for-loops and just numpy.reshape the sequence of dictionary keys

import numpy as np

rows = 3
cols = 4

d = { (i,j):[] for i in range(rows) for j in range(cols)}
A = np.array(list(d.keys())).reshape((rows,cols,2))

As you are basically creating a matrix with indices, you might consider to use the in-built function numpy.indices

A = np.dstack(np.indices((rows,cols)))

Some remarks

You are right with your assumption on the error you got when assigning an empty list to elements of A: it expects elements of type float. In your case the matrix you want to create has, in fact, three dimensions: A.shape is (3, 4, 2). Also, the iteration over all dictionary keys makes is the same as iterating over rows and columns - doing both at once makes no sense.

Marc
  • 712
  • 4
  • 7
  • Thank you, Sir, I do appreciate it. Additionally, I wish I found the `np.indices` earlier. That would make my life a lot easier. lol – uBoscuBo Jan 14 '21 at 15:43