1.

Python doesn't have concept of array as in C/C++/Java etc. But it does have array module in the standard library. How is it used?

Answer»

Array is a collection of more than one element of similar data type in C/C++/Java etc. Python doesn't have any built-in equivalent of array. Its List/tuple data types are COLLECTIONS, but items in it may be of different types.

Python's array module emulates C type array. The module defines 'array' class whose constructor requires two arguments:

array(typecode, initializer)

The typecode determines the type of array. Initializer should be a sequence with all elements of matching type.

Type codeC TypePython Type
'b'signed charint
'B'unsigned charint
'u'Py_UNICODEUnicode character
'h'signed shortint
'H'unsigned shortint
'i'signed intint
'I'unsigned intint
'I'signed longint
'L'unsigned longint
'q'signed long longint
'Q'unsigned long longint
'f'Floatfloat
'd'Doublefloat

The array module defines typecodes attribute which RETURNS a STRING. Each character in the string represents a type code.

>>> array.typecodes 'bBuhHiIlLqQfd'

Following statement creates an integer array object:

>>> import array >>> arr=array.array('i', range(5)) >>> arr array('i', [0, 1, 2, 3, 4]) >>> type(arr) <class 'array.array'>

The initializer may be a byte like object. Following example builds an array from byte representation of string.

>>> arr=array.array('b', b'Hello') >>> arr array('b', [72, 101, 108, 108, 111])

Some of the useful methods of the array class are as follows:

extend():

This method appends items from list/tuple to the end of the array. Data type of array and iterable list/tuple must MATCH; if not, TypeError will be raised.

>>> arr=array.array('i', [0, 1, 2, 3, 4]) >>> arr1=array.array('i',[10,20,30]) >>> arr.extend(arr1) >>> arr array('i', [0, 1, 2, 3, 4, 10, 20, 30])

fromfile():

This method reads data from the file object and appends to an array.

>>> a=array.array('i') >>> file=open('test.txt','rb') >>> a.fromfile(file,file.tell()) >>> a array('i', [1819043144, 2035294319, 1852794996])

tofile():

This method write all items to the file object which must have write mode enabled.

>>> a=array.array('i', [10, 20, 30, 40, 50]) >>> file=open("temp.txt","WB") >>> a.tofile(file) >>> file.close() >>> file=open("temp.txt","rb") >>> file.read() b'\n\x00\x00\x00\x14\x00\x00\x00\x1e\x00\x00\x00(\x00\x00\x002\x00\x00\x00'

append():

This method appends a new item to the end of the array

fromlist():

This method appends items from the list to array. This is equivalent to for x in list: a.append(x)

>>> a=array.array('i') >>> a.append(10) >>> a array('i', [10]) >>> num=[20,30,40,50] >>> a.fromlist(num) >>> a array('i', [10, 20, 30, 40, 50])

insert():

Insert a new item in the array before specified position

>>> a=array.array('i', [10, 20, 30, 40, 50]) >>> a.insert(2,25) >>> a array('i', [10, 20, 25, 30, 40, 50])

pop():

This method returns item at given index after removing it from the array.

>>> a=array.array('i', [10, 20, 30, 40, 50]) >>> x=a.pop(2) >>> x 30 >>> a array('i', [10, 20, 40, 50])

remove():

This method removes first occurrence of given item from the array.

>>> a=array.array('i', [10, 20, 30, 40, 50]) >>> a.remove(30) >>> a array('i', [10, 20, 40, 50])


Discussion

No Comment Found