Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allocate array size in Python [duplicate]

Tags:

python

arrays

Python newbie here. I've searched quite a bit for a solution to this but nothing quite fits what I need. I would like to allocate an empty array at the start of my program that has a rows and b columns. I came up with a solution but encountered an interesting problem that I didn't expect. Here's what I had:

a = 7
b = 5
array_ab = [['?'] * b] * a

which produces

[['?', '?', '?', '?', '?'],
 ['?', '?', '?', '?', '?'],
 ['?', '?', '?', '?', '?'],
 ['?', '?', '?', '?', '?'],
 ['?', '?', '?', '?', '?'],
 ['?', '?', '?', '?', '?'],
 ['?', '?', '?', '?', '?']]

However, if I try to change a single element, it treats every row as the same object and effectively changes the entire column to that element. So for example

array_ab[4][2] = '1'

produces

[['?', '?', '1', '?', '?'],
 ['?', '?', '1', '?', '?'],
 ['?', '?', '1', '?', '?'],
 ['?', '?', '1', '?', '?'],
 ['?', '?', '1', '?', '?'],
 ['?', '?', '1', '?', '?'],
 ['?', '?', '1', '?', '?']]

Clearly I need a better way to create the blank array than by multiplication. Is there a solution to this in python? (It was so simple in FORTRAN!)

like image 645
JohannesKepler Avatar asked Feb 05 '14 15:02

JohannesKepler


People also ask

How do you duplicate an array in Python?

We can create a copy of an array by using the assignment operator (=). In Python, Assignment statements do not copy objects, they create bindings between a target and an object. When we use = operator user thinks that this creates a new object; well, it doesn't.

How do you make an array the same size as a different array in Python?

For example, the following code creates an array of zeros with the same shape as the x input array: x = np. array([[1,2],[3,4],[5,6]], dtype=np.

How do you change the size of an array in Python?

Changing size of numpy Array in Python Size of a numpy array can be changed by using resize() function of the NumPy library. refcheck- It is a boolean that checks the reference count.

How do you dynamically increase the size of an array in Python?

The elements of an array occupy a contiguous block of memory, and once created, its size cannot be changed. A dynamic array can, once the array is filled, allocate a bigger chunk of memory, copy the contents from the original array to this new space, and continue to fill the available slots.


2 Answers

Something along the lines of

In [12]: a = 5

In [13]: b = 7

In [14]: array_ab = [ [ '?' for i in xrange(a) ] for j in xrange(b) ]

In [15]: array_ab
Out[15]:
[['?', '?', '?', '?', '?'],
 ['?', '?', '?', '?', '?'],
 ['?', '?', '?', '?', '?'],
 ['?', '?', '?', '?', '?'],
 ['?', '?', '?', '?', '?'],
 ['?', '?', '?', '?', '?'],
 ['?', '?', '?', '?', '?']]

In [16]: array_ab[4][2] = '1'

In [17]: array_ab
Out[17]:
[['?', '?', '?', '?', '?'],
 ['?', '?', '?', '?', '?'],
 ['?', '?', '?', '?', '?'],
 ['?', '?', '?', '?', '?'],
 ['?', '?', '1', '?', '?'],
 ['?', '?', '?', '?', '?'],
 ['?', '?', '?', '?', '?']]

In particular, you're using list comprehensions and xrange.

like image 135
Ehtesh Choudhury Avatar answered Sep 30 '22 23:09

Ehtesh Choudhury


If you are going to use your array for numerical computations, and can live with importing an external library, then I would suggest looking at numpy. It provides an array class and lots of useful array operations.

Creating an MxN array is simply

import numpy as np

A = np.empty((M,N)) # Empty array
B = np.zeros((M,N)) # Array filled with zeros

Indexing is then done like

x = A[i,j]
A[4,2] = 1

row1 = A[0, :] # or simply A[0]
like image 42
Hannes Ovrén Avatar answered Sep 30 '22 23:09

Hannes Ovrén