0

Not Yet Answered # python array and dictionary

pyTony 888 vegaseat 1,720 pyTony 888 ultimatebuster 14 vegaseat 1,720

0

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.

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

```
from turtle import *
import random
def allTriMedian (w=300):
speed (0)
vertices = []
point = turtle.Point(x,y)
for i in range (3):
x = random.randint(0,300)
y = random.randint(0,300)
vertices.append(trutle.Point(x,y))
point = turtle.Point(x,y)
triangle = turtle.Polygon(vertices)
a = triangle.side()
b = triangle.side()
c = triangle.side()
m1 = tirangle.median
m2 = triangle.median ...
```

Hi!

I have two tables, I will call them first-half and second-half (of the year)

```
Table: first
id jan feb mar apr
-----------------------
2 80 90 70 60
3 50 40 60 30
Table: second
id jan feb mar apr
-----------------------
2 50 70 60 90
3 80 60 50 ...
```

I'm trying to figure out how do i print a distinct enum value by using a conditional statement in my construction of array.

I have a enum declaration

`enum Animal {Rat, Ox, Tiger, Rabbit, Dragon, Snake, Horse, Sheep, Monkey, Rooster, Dog, Pig};`

And i construct my array using a voidptr ...