Python中的数组|集1(简介和函数)

除了一些通用容器,比如 列表 ,Python在其定义中还可以处理具有指定数据类型的容器。该数组可以由一个名为“”的模块在python中处理 大堆 “。当我们必须只操作特定的数据类型值时,它们可能很有用。

null

阵列上的操作:

1.数组(数据类型、值列表) :-此函数用于 创造 在其参数中指定了数据类型和值列表的数组。下表中提到了一些数据类型。

类型代码 C型 Python类型 最小大小(字节)
“b” 签名字符 智力 1.
“B” 无符号字符 智力 1.
“你” Py_UNICODE unicode字符 2.
“h” 短签名 智力 2.
“H” 未签名短 智力 2.
“我很高兴 有符号整型 智力 2.
“我很高兴 无符号整型 智力 2.
“我 长签名 智力 4.
“我 未签名长 智力 4.
“q” 签了很久 智力 8.
“Q” 无符号长 智力 8.
“f” 浮动 浮动 4.
“是的” 双重的 浮动 8.

2.附加() :-此函数用于 增加价值 在辩论中提到 终止 阵列的一部分。 3.插入(i,x) :-此函数用于 在第i个位置添加值(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 ( "
"
)
# 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 ( "
"
)
# printing array after insertion
print ( "The array after insertion is : " , end = "")
for i in range ( 0 , 5 ):
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.流行音乐 :-这个功能 移除该位置的元素 在其参数中提到并返回它。 5.删除() :-此函数用于 删除第一个引用 其参数中提到的值。

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 ( "
"
)
# 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 ( "
"
)
# 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 : 3The array after popping is : 1 2 1 5 The array after removing is : 2 1 5 

6.索引() :-此函数返回 首次出现的索引 在论点中提到的价值。 7.反向 :-这个功能 逆转 阵列。

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 ( "
"
)
# 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 : 1The array after reversing is : 5 2 1 3 2 1

参考: https://docs.python.org/3/library/array.html#module-阵列 本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享