1.11M Members

python array and dictionary

 
0
 

Hello,

I have been using the python dictionary to create a multidimensional array. I recently stumbled upon the numpy module's array which creates a multidimensional list. Can we store string variables in this array module? or is there other ways in python to create a multidimensional list?

Thanks

 
0
 

Give example data so we can discuss best data structure for it. Often dictionary does the job. Usong numpy arrays is possible but you must be carefull not to do too much conversions between numpy and normal lists. Often it is better to do calculations in numpy form but return results in standard library form, say list or tuple.

 
1
 

Numpy arrays allow for numeric elements of a certain type only. Those can be integer, float or complex numbers. You can convert a Numpy array to a Python list ...

# using Python module numpy to create a multi-dimensional array/list

import numpy as np

# create a 2x3 array of ones
xf = np.ones((2, 3), float)
print(xf)
'''
[[ 1.  1.  1.]
 [ 1.  1.  1.]]
'''

# convert to a 2x3 Python list
print(xf.tolist())
'''
[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]
'''

You can create Python multidimensional list this way ...

# creating a multi-dimensional list
# elements can be any object

multi_list = [[1 for col in range(3)] for row in range(2)]

print(multi_list)  # [[1, 1, 1], [1, 1, 1]] 

# use [row][col]
multi_list[0][1] = 2
multi_list[0][2] = 3
print(multi_list)  # [[1, 2, 3], [1, 1, 1]]

My advice, if you have certain number types and need the high speed and some nice array functions, use numpy. Otherwise go for the dictionary approach ...

# create a 3x4x2 3D-array using a dictionary with (x,y,z):value pairs

import pprint

# initializes values to zero
arr3d = dict(((x,y,z), 0) for x in range(3) for y in range(4)
    for z in range(2))

# Python3 has dictionary comprehension to simplify this
#arr3d = {(x,y,z):0 for x in range(3) for y in range(4) for z in range(2)}

pprint.pprint(arr3d)

"""my output -->
{(0, 0, 0): 0,
 (0, 0, 1): 0,
 (0, 1, 0): 0,
 (0, 1, 1): 0,
 (0, 2, 0): 0,
 (0, 2, 1): 0,
 (0, 3, 0): 0,
 (0, 3, 1): 0,
 (1, 0, 0): 0,
 (1, 0, 1): 0,
 (1, 1, 0): 0,
 (1, 1, 1): 0,
 (1, 2, 0): 0,
 (1, 2, 1): 0,
 (1, 3, 0): 0,
 (1, 3, 1): 0,
 (2, 0, 0): 0,
 (2, 0, 1): 0,
 (2, 1, 0): 0,
 (2, 1, 1): 0,
 (2, 2, 0): 0,
 (2, 2, 1): 0,
 (2, 3, 0): 0,
 (2, 3, 1): 0}
"""

print('-'*20)

# change some values, basically assign to index
arr3d[2, 1, 0] = 550
arr3d[1, 0, 1] = 1700
# or ...
ix = (0, 0, 0)
arr3d[ix] = 99
# or just ...
ix = 1, 1, 1
arr3d[ix] = 303

pprint.pprint(arr3d)

"""my output -->
{(0, 0, 0): 99,
 (0, 0, 1): 0,
 (0, 1, 0): 0,
 (0, 1, 1): 0,
 (0, 2, 0): 0,
 (0, 2, 1): 0,
 (0, 3, 0): 0,
 (0, 3, 1): 0,
 (1, 0, 0): 0,
 (1, 0, 1): 1700,
 (1, 1, 0): 0,
 (1, 1, 1): 303,
 (1, 2, 0): 0,
 (1, 2, 1): 0,
 (1, 3, 0): 0,
 (1, 3, 1): 0,
 (2, 0, 0): 0,
 (2, 0, 1): 0,
 (2, 1, 0): 550,
 (2, 1, 1): 0,
 (2, 2, 0): 0,
 (2, 2, 1): 0,
 (2, 3, 0): 0,
 (2, 3, 1): 0}
"""

print('-'*20)

# get a specific value from a given index
print(arr3d[1, 1, 1])  # 303
# or ...
ix = 0, 0, 0
print(arr3d[ix])  # 99

print('-'*20)

# get the lowest and highest key
low = min(arr3d.keys())    # --> (0, 0, 0)
heigh = max(arr3d.keys())  # --> (2, 3, 1)
# show values
print( "ix=%s val=%s" % (low, arr3d[low]) )
print( "ix=%s val=%s" % (heigh, arr3d[heigh]) )

# get the heighest value in the array
print(max(arr3d.values()))  # 1700
 
1
 

This dict approach is known more widely as 'sparse matrix' if you want to google around. It is useful when most values are zero, see for example my snippet:
http://www.daniweb.com/software-development/python/code/358014 (one dimensional, but that does not make any difference in this approach).

As you can see I use variation of dictionary called defaultdict, it is very useful to know use it. Makes your life easier.

 
0
 

It's worth noting that dictionaries are not the same as multidimensional array. They are similar, however a dictionary is actually a hash table.

 
0
 

Yes, using a dictionary will hash the index tuple, which then allows for extremely fast look-ups of nested data.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article