Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you create traditional fixed length and type arrays in Python?

Tags:

python

I am teaching the A level syllabus by the Cambridge exam board. One typical question on the practical paper is this:

In a programming language of your choice, declare an array of ten integers and then initializes it.

I have this:

myArray = []        #create the array

for i in range(10): # initializes to 0
    myArray.append(0)

I believe this is what most people would do in Python? However, unlike Pascal etc it does not strictly answer the question as the lists in Python are essentially dynamic arrays with the added problem of not restricting the data type of each element. Can I declare a specific size array and type like in Pascal, instead of an empty dynamic list, without any type of loop and restricts the type of the array?

Something like (pseudo code):

myArray[10] : integer

This is a none commercial question but an educational one.

like image 341
Timothy Lawman Avatar asked Oct 22 '12 11:10

Timothy Lawman


People also ask

Can Python implement arrays in their traditional form?

However, unlike other programming languages, arrays aren't a built-in data structure in Python. Instead of traditional arrays, Python uses lists. Lists are essentially dynamic arrays and are one of the most common and powerful data structures in Python.

Are arrays in Python fixed size?

NumPy arrays have a fixed size at creation, unlike Python lists (which can grow dynamically). Changing the size of an ndarray will create a new array and delete the original. The elements in a NumPy array are all required to be of the same data type, and thus will be the same size in memory.

Do arrays have a fixed length?

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.

Can Python arrays hold different types?

Can array in Python store different data types? No, we cannot store multiple datatype in an Array, we can store similar datatype only in an Array.


1 Answers

You could try to use the array module to specify the type of your array:

import array
a = array.array('i') # Define an integer array.

You can then add the elements you want to the array. I'm not sure whether you can predefine the size your array should have, though. If you want an array of ten integer elements, each element being zero, you could do:

a = array.array('i', [0]*10)

As described in the documentation, the 'i' forces the elements of the array to be integers. Python 2.6 will throw a DeprecationWarning if you try to insert a float in an array of integers, but will cast the float as an int:

>>> a[0]=3.14159
>>> a
>>> array('i', [3, 0, 0, 0, 0, 0, 0, 0, 0, 0])

Alternatively, you could use the numpy package, which lets you define both the size and the type of the array.

import numpy as np
a = np.empty(10, dtype=int) # Define a integer array with ten elements

The np.empty just reserves some space in memory for the array, it doesn't initialize it. If you need an array of 0, you could do:

a[:] = 0

or directly use the np.zeros function:

a = np.zeros(10, dtype=int)

Here again, inserting a float in an array of integers will silently convert the float to integer.

Note a difference between numpy and array: once you define an array in numpy, you cannot change its size without having to recreate an array. In that sense, it satisfies your requirement of "10 and only 10 integers". By contrast, a array.array object can be seen as a list with a fixed element type: the array is dynamic, you can increase its size.

like image 182
Pierre GM Avatar answered Sep 18 '22 13:09

Pierre GM