📜  Python中的数组|第一组(介绍及功能)

📅  最后修改于: 2022-05-13 01:55:37.009000             🧑  作者: Mango

Python中的数组|第一组(介绍及功能)

除了一些像list这样的通用容器外, Python在其定义中还可以处理具有指定数据类型的容器。数组可以在Python中由名为“ array ”的模块处理。当我们只需要操作特定的数据类型值时,它们会很有用。

数组操作:

1. array(data type, value list) :- 该函数用于创建一个数组,其参数中指定了数据类型和值列表。下表中提到了一些数据类型。

Type CodeC TypePython TypeMinimum size in Bytes
‘b’signed charint1
‘B’unsigned charint1
‘u’Py_UNICODEunicode character2
‘h’signed shortint2
‘H’unsigned shortint2
‘i’signed intint2
‘I’unsigned intint2
‘l’signed longint4
‘L’unsigned longint4
‘q’signed long longint8
‘Q’unsigned long longint8
‘f’floatfloat4
‘d’doublefloat8

2. append() :- 该函数用于将其参数中提到的值添加到数组的末尾
3. insert(i,x) :- 此函数用于在其参数中指定的第 i 个位置添加 value(x)

Python3
# Python code to demonstrate the working of
# array(), append(), insert()
  
# importing "array" for array operations
import array
  
# initializing array with array values
# initializes array with signed integers
arr = array.array('i', [1, 2, 3])
 
# printing original array
print ("The new created array is : ",end=" ")
for i in range (0, 3):
    print (arr[i], end=" ")
 
print("\r")
 
# using append() to insert new value at end
arr.append(4);
 
# printing appended array
print("The appended array is : ", end="")
for i in range (0, 4):
    print (arr[i], end=" ")
     
# using insert() to insert value at specific position
# inserts 5 at 2nd position
arr.insert(2, 5)
 
print("\r")
 
# printing array after insertion
print ("The array after insertion is : ", end="")
for i in range (0, 5):
    print (arr[i], end=" ")


Python3
# Python code to demonstrate the working of
# pop() and remove()
 
# 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=" ")


Python3
# Python code to demonstrate the working of
# index() and reverse()
  
# 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, 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 reverse() to reverse the array
arr.reverse()
 
# printing array after reversing
print ("The array after reversing is : ",end="")
for i in range (0,6):
    print (arr[i],end=" ")


输出:

The new created array is : 1 2 3 
The appended array is : 1 2 3 4 
The array after insertion is : 1 2 5 3 4 

4. pop() :- 这个函数移除参数中提到的位置的元素并返回它。
5. remove() :- 此函数用于删除其参数中提到的值的第一次出现

Python3

# Python code to demonstrate the working of
# pop() and remove()
 
# 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 

6. index() :- 该函数返回参数中提到的值第一次出现的索引
7. reverse() :- 这个函数反转数组。

Python3

# Python code to demonstrate the working of
# index() and reverse()
  
# 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, 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 reverse() to reverse the array
arr.reverse()
 
# printing array after reversing
print ("The array after reversing is : ",end="")
for i in range (0,6):
    print (arr[i],end=" ")

输出:

The new created array is : 1 2 3 1 2 5 
The index of 1st occurrence of 2 is : 1
The array after reversing is : 5 2 1 3 2 1