数组是存储在连续内存位置的项目的集合。这个想法是将多个相同类型的项目存储在一起。这使得通过简单地向基值添加偏移量来计算每个元素的位置变得更容易,即,数组的第一个元素的内存位置(通常由数组的名称表示)。 为简单起见,我们可以将一个数组想象成一组楼梯,其中每一步都放置一个值(假设您的一个朋友)。在这里,您可以通过简单地了解他们所走的步数来确定您的任何朋友的位置。数组可以在 Python 中由名为array的模块处理。当我们只需要操作特定数据类型的值时,它们会很有用。用户可以处理列表作为数组。但是,用户不能限制存储在列表中的元素的类型。如果使用array模块创建数组,则数组的所有元素必须属于同一类型。
Python 中的数组可以通过导入 array 模块来创建。array( data_type , value_list )用于创建一个数组,其参数中指定了数据类型和值列表。
# Python program to demonstrate
# Creation of Array
# importing "array" for array creations
import array as arr
# creating an array with integer type
a = arr.array('i', [1, 2, 3])
# printing original array
print("The new created array is : ", end=" ")
for i in range(0, 3):
print(a[i], end=" ")
print()
# creating an array with double type
b = arr.array('d', [2.5, 3.2, 3.3])
# printing original array
print("\nThe new created array is : ", end=" ")
for i in range(0, 3):
print(b[i], end=" ")
输出
The new created array is : 1 2 3
The new created array is : 2.5 3.2 3.3
输出 :
The new created array is : 1 2 3
The new created array is : 2.5 3.2 3.3
时间复杂度: O(1)
辅助空间: O(n)
下面提到了一些数据类型,这将有助于创建不同数据类型的数组。
可以使用内置的insert()函数将元素添加到数组中。Insert 用于将一个或多个数据元素插入到数组中。根据需要,可以在数组的开头、结尾或任何给定索引处添加新元素。append()还用于在数组末尾添加其参数中提到的值。
# Python program to demonstrate
# Adding Elements to a Array
# importing "array" for array creations
import array as arr
# array with int type
a = arr.array('i', [1, 2, 3])
print("Array before insertion : ", end=" ")
for i in range(0, 3):
print(a[i], end=" ")
print()
# inserting array using
# insert() function
a.insert(1, 4)
print("Array after insertion : ", end=" ")
for i in (a):
print(i, end=" ")
print()
# array with float type
b = arr.array('d', [2.5, 3.2, 3.3])
print("Array before insertion : ", end=" ")
for i in range(0, 3):
print(b[i], end=" ")
print()
# adding an element using append()
b.append(4.4)
print("Array after insertion : ", end=" ")
for i in (b):
print(i, end=" ")
print()
输出
Array before insertion : 1 2 3
Array after insertion : 1 4 2 3
Array before insertion : 2.5 3.2 3.3
Array after insertion : 2.5 3.2 3.3 4.4
输出 :
Array before insertion : 1 2 3
Array after insertion : 1 4 2 3
Array before insertion : 2.5 3.2 3.3
Array after insertion : 2.5 3.2 3.3 4.4
时间复杂度: O(1)/O(n) ( O(1) – 用于在数组末尾插入元素,O(n) – 用于在数组开头和整个数组中插入元素
辅助空间: O(1)
为了访问数组项,请参考索引号。使用索引运算符 [ ] 访问数组中的项目。索引必须是整数。
# Python program to demonstrate
# accessing of element from list
# importing array module
import array as arr
# array with int type
a = arr.array('i', [1, 2, 3, 4, 5, 6])
# accessing element of array
print("Access element is: ", a[0])
# accessing element of array
print("Access element is: ", a[3])
# array with float type
b = arr.array('d', [2.5, 3.2, 3.3])
# accessing element of array
print("Access element is: ", b[1])
# accessing element of array
print("Access element is: ", b[2])
输出
Access element is: 1
Access element is: 4
Access element is: 3.2
Access element is: 3.3
输出 :
Access element is: 1
Access element is: 4
Access element is: 3.2
Access element is: 3.3
时间复杂度: O(1)
辅助空间: O(1)
可以使用内置的remove()函数从数组中删除元素,但如果集合中不存在元素,则会出现错误。Remove() 方法一次只删除一个元素,要删除范围内的元素,使用迭代器。pop()函数也可用于从数组中移除和返回一个元素,但默认情况下它只移除数组的最后一个元素,要从数组的特定位置移除元素,元素的索引作为pop() 方法的参数。 注意 – List 中的 Remove 方法只会删除第一次出现的搜索元素。
# Python program to demonstrate
# Removal of elements in a Array
# importing "array" for array operations
import array
# initializing array with array values
# initializes array with signed integers
arr = array.array('i', [1, 2, 3, 1, 5])
# printing original array
print("The new created array is : ", end="")
for i in range(0, 5):
print(arr[i], end=" ")
print("\r")
# using pop() to remove element at 2nd position
print("The popped element is : ", end="")
print(arr.pop(2))
# printing array after popping
print("The array after popping is : ", end="")
for i in range(0, 4):
print(arr[i], end=" ")
print("\r")
# using remove() to remove 1st occurrence of 1
arr.remove(1)
# printing array after removing
print("The array after removing is : ", end="")
for i in range(0, 3):
print(arr[i], end=" ")
输出
The new created array is : 1 2 3 1 5
The popped element is : 3
The array after popping is : 1 2 1 5
The array after removing is : 2 1 5
输出:
The new created array is : 1 2 3 1 5
The popped element is : 3
The array after popping is : 1 2 1 5
The array after removing is : 2 1 5
时间复杂度: O(1)/O(n)(O(1) – 用于删除数组末尾的元素,O(n) – 用于删除数组开头和整个数组的元素
辅助空间: O(1)
在 Python 数组中,有多种方法可以打印包含所有元素的整个数组,但是要打印数组中特定范围的元素,我们使用Slice 操作。使用冒号(:)对数组执行切片操作。要打印从开始到范围的元素,使用 [:Index],打印从结束到结束的元素,使用 [:-Index],打印从特定索引到结束的元素,使用 [Index:],打印范围内的元素,使用 [ Start Index:End Index] 并使用切片操作打印整个列表,使用 [:]。此外,要以相反的顺序打印整个数组,请使用 [::-1]。
# Python program to demonstrate
# slicing of elements in a Array
# importing array module
import array as arr
# creating a list
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a = arr.array('i', l)
print("Initial Array: ")
for i in (a):
print(i, end=" ")
# Print elements of a range
# using Slice operation
Sliced_array = a[3:8]
print("\nSlicing elements in a range 3-8: ")
print(Sliced_array)
# Print elements from a
# pre-defined point to end
Sliced_array = a[5:]
print("\nElements sliced from 5th "
"element till the end: ")
print(Sliced_array)
# Printing elements from
# beginning till end
Sliced_array = a[:]
print("\nPrinting all elements using slice operation: ")
print(Sliced_array)
输出
Initial Array:
1 2 3 4 5 6 7 8 9 10
Slicing elements in a range 3-8:
array('i', [4, 5, 6, 7, 8])
Elements sliced from 5th element till the end:
array('i', [6, 7, 8, 9, 10])
Printing all elements using slice operation:
array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
为了搜索数组中的元素,我们使用 Python 内置的index()方法。此函数返回参数中提到的值第一次出现的索引。
# Python code to demonstrate
# searching an element in array
# importing array module
import array
# initializing array with array values
# initializes array with signed integers
arr = array.array('i', [1, 2, 3, 1, 2, 5])
# printing original array
print("The new created array is : ", end="")
for i in range(0, 6):
print(arr[i], end=" ")
print("\r")
# using index() to print index of 1st occurrence of 2
print("The index of 1st occurrence of 2 is : ", end="")
print(arr.index(2))
# using index() to print index of 1st occurrence of 1
print("The index of 1st occurrence of 1 is : ", end="")
print(arr.index(1))
输出
The new created array is : 1 2 3 1 2 5
The index of 1st occurrence of 2 is : 1
The index of 1st occurrence of 1 is : 0
输出:
The new created array is : 1 2 3 1 2 5
The index of 1st occurrence of 2 is : 1
The index of 1st occurrence of 1 is : 0
时间复杂度: O(n)
辅助空间: O(1)
为了更新数组中的元素,我们只需将一个新值重新分配给我们想要更新的所需索引。
# Python code to demonstrate
# how to update an element in array
# importing array module
import array
# initializing array with array values
# initializes array with signed integers
arr = array.array('i', [1, 2, 3, 1, 2, 5])
# printing original array
print("Array before updation : ", end="")
for i in range(0, 6):
print(arr[i], end=" ")
print("\r")
# updating a element in a array
arr[2] = 6
print("Array after updation : ", end="")
for i in range(0, 6):
print(arr[i], end=" ")
print()
# updating a element in a array
arr[4] = 8
print("Array after updation : ", end="")
for i in range(0, 6):
print(arr[i], end=" ")
输出
Array before updation : 1 2 3 1 2 5
Array after updation : 1 2 6 1 2 5
Array after updation : 1 2 6 1 8 5
输出:
Array before updation : 1 2 3 1 2 5
Array after updation : 1 2 6 1 2 5
Array after updation : 1 2 6 1 8 5
时间复杂度: O(n)
辅助空间: O(1)
为了计算数组中的元素,我们需要使用 count 方法。
import array
# Create an array of integers
my_array = array.array('i', [1, 2, 3, 4, 2, 5, 2])
# Count the number of occurrences of the element 2 in the array
count = my_array.count(2)
# Print the result
print("Number of occurrences of 2:", count)
输出
Number of occurrences of 2: 3
时间复杂度: O(n)
辅助空间: O(1)
为了反转数组的元素,我们需要简单地使用 reverse 方法。
import array
# Create an array of integers
my_array = array.array('i', [1, 2, 3, 4, 5])
# Print the original array
print("Original array:", *my_array)
# Reverse the array in place
my_array.reverse()
# Print the reversed array
print("Reversed array:", *my_array)
输出
Original array: 1 2 3 4 5
Reversed array: 5 4 3 2 1
时间复杂度: O(n)
空间: O(1)
在本文中,我们将介绍 python 列表extend()并尝试理解Python 列表 extend()。
在 Python 中,数组用于在单个变量中存储多个值或相同数据类型的元素。extend ()函数只是用来将可迭代对象中的一个项目附加到数组的末尾。简单来说,此方法用于将值数组添加到给定或现有数组的末尾。
extend() 方法的语法:
list.extend(iterable)
这里,iterable的所有元素都被添加到list1的末尾
#Python program to demonstrate
# Adding Elements to a Array
# importing "array" for array creations
import array as arr
# array with int type
a = arr.array('i', [1, 2, 3,4,5])
#printing original array
print("The before array extend : ", end =" ")
for i in range (0, 5):
print (a[i], end =" ")
print()
#creating an array with using extend method
a.extend([6,7,8,9,10])
#printing original array
print("\nThe array after extend :",end=" ")
for i in range(0,10):
print(a[i],end=" ")
print()
输出
The before array extend : 1 2 3 4 5
The array after extend : 1 2 3 4 5 6 7 8 9 10
#Python program to demonstrate
# Creation of Array
# importing "array" for array creations
import array as arr
#creating an array with integer type
a=arr.array('i',[1,2,3,4,5,6])
# printing original array
print("The Before extend array is :",end=" ")
for i in range(0,6):
print(a[i],end=" ")
print()
# creating an array with using extend method
a.extend([7,8,9,10,11,12])
# printing original array
print("\nThe After extend array is :",end=" ")
for i in range(0,12):
print(a[i],end=" ")
print()
#array with float type
b = arr.array('d', [2.1,2.2,2.3,2.4,2.5,2.6])
print("\nThe before extend array is :",end=" ")
for i in range(0,6):
print(b[i],end=" ")
print()
#extend function using pass the elements
b.extend([2.6,2.7,2.8,2.9])
print("\nThe after extend array is :",end=" ")
for i in range(0,9+1):
print(b[i],end=" ")
print()
输出
The Before extend array is : 1 2 3 4 5 6
The After extend array is : 1 2 3 4 5 6 7 8 9 10 11 12
The before extend array is : 2.1 2.2 2.3 2.4 2.5 2.6
The after extend array is : 2.1 2.2 2.3 2.4 2.5 2.6 2.6 2.7 2.8 2.9
时间复杂度:O(1)
辅助空间: O(1)
原文链接:codingdict.net