English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn about Python arrays, the differences between arrays and lists, and how and when to use them with examples.
In programming, an array is a collection of elements of the same type.
arrays in Java, C / C ++It is very popular in most programming languages such as JavaScript. However, in Python, they are not common. When people talk about Python arrays, they are actually talking about Python lists. If you don't know what a list is, you should definitely check outPython listarticle.
We can consider a list as an array. However, we cannot restrict the type of elements stored in the list. For example:
a = [1, 3.5, "Hello"]
If an array is created using the array module, all elements of the array must be of the same numeric type.
import array as arr a = arr.array('d', [1, 3.5, "Hello"]) // Error
You may have guessed from the above example that we need to import the array module to create an array. For example:
import array as arr a = arr.array('d', [1.1, 3.5, 4.5]) print(a)
Here, we create a float type array. The letter 'd' is the type code. This determines the type of the array during the creation process.
Common type codes:
Code | C Type | Python Type | Minimum byte |
---|---|---|---|
'b' | signed char | int | 1 |
'B' | unsigned char | int | 1 |
'u' | Py_UNICODE | Unicode | 2 |
'h' | signed short | int | 2 |
'H' | unsigned short | int | 2 |
'i' | signed int | int | 2 |
'I' | unsigned int | int | 2 |
'l' | signed long | int | 4 |
'L' | unsigned long | int | 4 |
'f' | float | float | 4 |
'd' | double | float | 8 |
We will not discuss different C types in this article. Throughout the article, we will use two type codes: 'i' for integers and 'd' for floating-point numbers.
Note: 'u' since version3.3starting, it is not recommended to use the type code for Unicode characters. Try to avoid using it.
We use indices to access elements of an array:
import array as arr a = arr.array('i', [2, 4, 6, 8]) print("First element:", a[0]) print("Second element:", a[1]) print("Last element:", a[-1])
Remember, the index starts from 0 (not1)beginning, similar to a list.
We can use the slicing operator to access a series of items in an array:.
import array as arr numbers_list = [2, 5, 62, 5, 42, 52, 48, 5] numbers_array = arr.array('i', numbers_list) print(numbers_array[2:5]) # The3-the5elements print(numbers_array[:-5]) # From the beginning to the4elements print(numbers_array[5:]) # From the6elements to the end print(numbers_array[:]) # From the beginning to the end
When running the program, the output is:
array('i', [62, 5, 42]) array('i', [2, 5, 62]) array('i', [52, 48, 5]) array('i', [2, 5, 62, 5, 42, 52, 48, 5])
Arrays are mutable; their elements can be changed in a way similar to lists.
import array as arr numbers = arr.array('i', [1, 2, 3, 5, 7, 10]) # Change the first element numbers[0] = 0 print(numbers) # Output: array('i', [0, 2, 3, 5, 7, 10]) # Change the3to the5elements numbers[2:5]) = arr.array('i', [4, 6, 8]) print(numbers) # Output: array('i', [0, 2, 4, 6, 8, 10])
We can use the append() method to add an item to a list, or use the extend() method to add multiple items to a 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 an 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+The operator connects two arrays.
import array as arr odd = arr.array('i', [1, 3, 5]) even = arr.array('i', [2, 4, 6]) numbers = arr.array('i') # Create an empty integer array numbers = odd + even print(numbers)
We can usePython's del statementRemove one or more items from an array.
import array as arr number = arr.array('i', [1, 2, 3, 3, 4]) del number[2] # Delete the third element print(number) # Output: array('i', [1, 2, 3, 4]) del number # Delete the entire array print(number) # Error: array is not defined
We can use the remove() method to delete a given item, or use the method to delete the item at a specified index pop().
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])
lists are much more flexible than arrays. They can store elements of different data types, including strings. Moreover, lists are faster than arrays. Moreover, if you need to perform mathematical calculations on arrays and matrices, it is best to useNumPymethods of libraries and the like.
Do not use them unless you really need an array (it may be necessary to use the array module with C code interface).