Maybe we can learn Python, I've only heard of lists (list)、 Tuples (tuple)、 aggregate (set)、 Dictionaries (dict)
That's popular in Java、C++ and C The common one in “ Tough people ”—— Where's the array ?
This article will take you to learn
- What are the differences between arrays and lists
- When and how to use arrays ?
In programming , An array is The same type Collection of elements of . Stored in Continuous memory Location .
however , stay Python in , They are not common .
When people talk Python Array time , They're talking about Python list . If you don't know what a list is , Find out for yourself .
Or wait for me to write an article Python List articles .Python The official is considerate , It provides us with an efficient numerical array ——array modular
Import this module first , Then check the functions under this module :
In [1]: import array
In [2]: dir(array)
Out[2]:
['ArrayType',
'__doc__',
'__file__',
'__loader__',
'__name__',
'__package__',
'__spec__',
'_array_reconstructor',
'array',
'typecodes']
list vs. array Modular array
We can use a list as an array , But don't forget the definition of array —— The same type . Yes , In the list , We can't limit the type of element .
>>>a = [1, 2, 'Hello']
If you use array Module creates an array , Then all elements of the array must be of the same numeric type . Grammatical structure :
a=arr.array(data type,value list)
>>> import array as arr
>>> a = arr.array('d', [1, 2, 'Hello'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be real number, not str
How to create an array ?
First , We need to import the array module to create an array .
for example :
>>> import array as arr
>>> a = arr.array('d', [1.1, 2.3, 4.8])
>>> print(a)
array('d', [1.1, 2.3, 4.8])
ad locum , We created one float An array of types . Letter “ d” Is the type code . This determines the type of array in the creation process .
Common type codes :
notes :'u' The type code corresponds to Python Outdated in unicode character (Py_UNICODE namely wchar_t). Depending on the system platform , It may be 16 Bit or 32 position . As of version 3.3 rise ,'u' Will work with other Py_UNICODE API Removed together
We will not discuss different in this article C type . In the whole article , We will use two types of code :
'i'
Represents an integer ,
' d'
Represents a floating point number .
Basic array operations :
How to access array elements ?
Support indexing like a list , Index value from 0 Start , It also supports circular access :
>>> import array as arr
>>> a = arr.array('i', [2, 4, 6, 8, 10])
>>> print('First element:', a[0])
First element: 2
>>> for i in a:
... print(i)
...
2
4
6
8
10
How to slice ?
Use slicing operators
:
, As long as it is Python The sequence type of , Just take out this knife .
>>> import array as arr
>>> a = arr.array('i', [2, 4, 6, 8, 10])
>>> print(a[2:5])
array('i', [6, 8, 10]) # from 3rd To 5th
>>> print(a[:-3]) # from 1st To 2nd
array('i', [2, 4])
>>> print(a[:]) # Full replication
array('i', [2, 4, 6, 8, 10])
>>> print(a[::-1]) # reverse
array('i', [10, 8, 6, 4, 2])
How to change or add elements ?
The array is variable ; Their elements can be changed in a list like manner .
import array as arr
numbers = arr.array('i', [1, 2, 3, 5, 7, 10])
# changing first element
numbers[0] = 0
print(numbers) # Output: array('i', [0, 2, 3, 5, 7, 10])
# changing 3rd to 5th element
numbers[2:5] = arr.array('i', [4, 6, 8])
print(numbers) # Output: array('i', [0, 2, 4, 6, 8, 10])
We can use
append()
Method to add an item to the list , Or use
extend()
Method to add multiple items to the list .
import array as arr
numbers = arr.array('i', [1, 2, 3])
numbers.append(4)
print(numbers) # Output: array('i', [1, 2, 3, 4])
# extend() appends iterable to the end of the array
numbers.extend([5, 6, 7])
print(numbers) # Output: array('i', [1, 2, 3, 4, 5, 6, 7])
We can use
+
Operator to connect two arrays .
import array as arr
odd = arr.array('i', [1, 3, 5])
even = arr.array('i', [2, 4, 6])
numbers = arr.array('i') # creating empty array of integer
numbers = odd + even
print(numbers)
how remove/delete Elements ?
We can use Python Of
del
Statement to delete one or more items from the array .
import array as arr
number = arr.array('i', [1, 2, 3, 3, 4])
del number[2] # removing third element
print(number) # Output: array('i', [1, 2, 3, 4])
del number # deleting entire array
print(number) # Error: array is not defined
We can use
remove()
Method to delete the given item ,
pop()
Deletes the entry at the given index .
import array as arr
numbers = arr.array('i', [10, 11, 12, 12, 13])
numbers.remove(12)
print(numbers) # Output: array('i', [10, 11, 12, 13])
print(numbers.pop(2)) # Output: 12
print(numbers) # Output: array('i', [10, 11, 13])
summary : When to use arrays ?
Lists are much more flexible than arrays . They can store elements of different data types , Including strings . and , Lists are faster than arrays . and , If you need to do mathematical calculations on arrays and matrices , It's better to use NumPy Method of array in Library .
Unless you need to talk to C Code interface , At this point, you need to use the array module , Otherwise don't use them .
Ps: This article is based on Python 3.8 Recommended reading :python-arraysarrays-in-python