2D arrays in Python

By : syaz
Source: Stackoverflow.com

What's the best way to create 2D arrays in Python?

What I want is want is to store values like this:

X , Y , Z

so that I access data like X[2],Y[2],Z[2] or X[n],Y[n],Z[n] where n is variable. I don't know in the beginning how big n would be so I would like to append values at the end.

By : syaz


Please consider the follwing codes:

from numpy import zeros
scores = zeros((len(chain1),len(chain2)), float)
By : C-Zeous

If you are concerned about memory footprint, the Python standard library contains the array module; these arrays contain elements of the same type.

By : EOL

I would suggest that you use a dictionary like so:

arr = {}

arr[1] = (1, 2, 4)
arr[18] = (3, 4, 5)

>>> (1, 2, 4)

If you're not sure an entry is defined in the dictionary, you'll need a validation mechanism when calling "arr[x]", e.g. try-except.

This video can help you solving your question :)
By: admin