Multidimensional Array using a Dictionary (Python)

vegaseat 1 Tallied Votes 7K Views Share

The example shows how to establish a dictionary of (row, column):value pairs to mimic a two dimensional array. This can be easily expanded to more dimensions. Python3 has dictionary comprehension, making the process of creating a dictionary of this type easier.

# use Python3's dictionary comprehension to mimic
# a 2 dimensional array with a dictionary
# with (row, column) index tuples as keys
# tested with Python 3.1.2 by vegaseat

def print_sortkey(d):
    """
    print dictionary d with keys sorted
    """
    for key in sorted(d):
        print( "%s --> %s" % (key, d[key]) )
    

# create a 3 x 4 2D-array using a dictionary with index tuples 
# as keys, initialize with zero
d_arr = {(x,y):0 for x in range(3) for y in range(4)} 

print( d_arr )
print( '-'*20 )

"""my result (tuples are in hash order) -->
{(0, 1): 0, (1, 2): 0, (0, 0): 0, (2, 1): 0, (0, 2): 0, ...}
"""

# change some values, basically assign to index
d_arr[2, 1] = 5
# or ...
row = 1
col = 0
d_arr[row, col] = 7
# or ...
ix = (0, 0)
d_arr[ix] = 9

print_sortkey( d_arr )
print( '-'*20 )

"""my result (sorted by index tuple) -->
(0, 0) --> 9
(0, 1) --> 0
(0, 2) --> 0
(0, 3) --> 0
(1, 0) --> 7
(1, 1) --> 0
(1, 2) --> 0
(1, 3) --> 0
(2, 0) --> 0
(2, 1) --> 5
(2, 2) --> 0
(2, 3) --> 0
"""

# get a specific value from a given index tuple
print( d_arr[0, 0] )  # 9
# or ...
row = 0
col = 0
print( d_arr[row, col] )  # 9
# or ... 
ix = (0, 0)
print( d_arr[ix] )  # 9
Skrell 0 Light Poster

First of all i LOVE these code snippets you did for various Python topics, i find them EXTREMELY helpful. With that said, i'm wondering what does the term "hash order" mean? I've seen the word "hashable" used several times now with Python and do not understand what it is referring to.

Ene Uran 638 Posting Virtuoso

In general a hashing algorithm turns a hashable object (immutable) into an integer value that is much faster to search.

Skrell 0 Light Poster

I'm sorry, i don't see how your explanation lends itself to the phrase, "(tuples are in hash order)"

RLS0812 -3 Newbie Poster
print( "%s --> %s" % (key, d[key]) )

Is incorrect, and will not run.
Corrected version:

ar = { (x,y,z):0                            # creating the array
       for x in range(3)
       for y in range (3)
       for z in range (3)} 
ar [2,1,0] = 777                            # random value
ar [2,2,1] = 2345                           # random value
for x in sorted(ar):                        #output
    print(str(x) + " --- " + str(ar[x]))    #print output
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

This works just fine with Python27 or Python32 ...

ar = { (x,y,z):0
       for x in range(3)
       for y in range(3)
       for z in range(3)} 

ar[2,1,0] = 777 
ar[2,2,1] = 2345

for key in sorted(ar):
    print("%s --> %s" % (key, ar[key]))

'''my result -->
(0, 0, 0) --> 0
(0, 0, 1) --> 0
(0, 0, 2) --> 0
(0, 1, 0) --> 0
(0, 1, 1) --> 0
(0, 1, 2) --> 0
(0, 2, 0) --> 0
(0, 2, 1) --> 0
(0, 2, 2) --> 0
(1, 0, 0) --> 0
(1, 0, 1) --> 0
(1, 0, 2) --> 0
(1, 1, 0) --> 0
(1, 1, 1) --> 0
(1, 1, 2) --> 0
(1, 2, 0) --> 0
(1, 2, 1) --> 0
(1, 2, 2) --> 0
(2, 0, 0) --> 0
(2, 0, 1) --> 0
(2, 0, 2) --> 0
(2, 1, 0) --> 777
(2, 1, 1) --> 0
(2, 1, 2) --> 0
(2, 2, 0) --> 0
(2, 2, 1) --> 2345
(2, 2, 2) --> 0
'''
Skrell 0 Light Poster

I am using python27 and neither works for me. I get the following error: Message File Name Line Position
Traceback
<module> <module1> 23
NameError: name 'ar' is not defined

Founder_1 0 Newbie Poster

How would you implement a linear interpolation using the same syntax?

Gunjan_2 0 Newbie Poster

How to convert this multidimensional array into a matrix with (x,y) will be x and y co-ordinate and value as element values?

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

You need to explain your request better.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.