# Python数组Array用法经典指南

2021年3月10日16:23:20 发表评论 1,038 次浏览

Array

。当我们只需要操作特定的数据类型值时, 它们就很有用。用户可以治疗

Array

#### 创建一个数组

Python中的数组可以通过导入来创建Array模块。数组(数据类型, 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 float type
b = arr.array( 'd' , [ 2.5 , 3.2 , 3.3 ])

# printing original array
print ( "The 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``````

#### 将元素添加到数组

``````# 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``````

#### 从数组访问元素

``````# 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``````

#### 从阵列中删除元素

``````# 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``````

#### 数组切片

。使用冒号(:)对数组执行切片操作。要从开始到范围内打印元素, 请使用[：Index], 要从最终位置使用[：-Index]打印元素, 要从特定索引开始打印直到最后, 使用[Index：], 要打印范围内的元素, 请使用[开始索引：结束索引], 并使用切片操作打印整个列表, 请使用[：]。此外, 要以相反的顺序打印整个数组, 请使用[::-1]。

``````# Python program to demonstrate
# silicing 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 ( "Intial 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)``````

``````Intial 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 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 occurrenece of 2
print ( "The index of 1st occurrence of 2 is : " , end = "")
print (arr.index( 2 ))

# using index() to print index of 1st occurrenece 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``````

#### 更新数组中的元素

``````# 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``````