Python中的数组|集2(重要函数)

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

null

下面是更多的功能。

1.类型代码 :-这个功能 返回数据类型 通过它初始化数组。

2.物品大小 :-此函数返回 大小 以字节为单位 单个数组元素。

3.缓冲区信息() :-返回表示 存储数组的地址和元素数 在里面。

# Python code to demonstrate the working of
# typecode, itemsize, buffer_info()
# 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 ])
# using typecode to print datatype of array
print ( "The datatype of array is : " )
print (arr.typecode)
# using itemsize to print itemsize of array
print ( "The itemsize of array is : " )
print (arr.itemsize)
# using buffer_info() to print buffer info. of array
print ( "The buffer info. of array is : " )
print (arr.buffer_info())


输出:

The datatype of array is : 
i
The itemsize of array is : 
4
The buffer info. of array is : 
(29784224, 6)

我们也可以使用*来导入数组,而不是导入数组。

# importing "array" using * for array operations
from array import *
# initializing array with array values
# initializes array with signed integers
arr = array( 'i' ,[ 1 , 2 , 3 , 1 , 2 , 5 ])
print (arr)


输出:

array('i', [1, 2, 3, 1, 2, 5])

4.计数() :-这个功能 统计发生的次数 数组中提到的参数。

5.扩展(arr) :-这个功能 追加整个数组 在指定数组的参数中提到。

# Python code to demonstrate the working of
# count() and extend()
# importing "array" for array operations
import array
# initializing array 1 with array values
# initializes array with signed integers
arr1 = array.array( 'i' ,[ 1 , 2 , 3 , 1 , 2 , 5 ])
# initializing array 2 with array values
# initializes array with signed integers
arr2 = array.array( 'i' ,[ 1 , 2 , 3 ])
# using count() to count occurrences of 1 in array
print ( "The occurrences of 1 in array is : " )
print (arr1.count( 1 ))
# using extend() to add array 2 elements to array 1
arr1.extend(arr2)
print ( "The modified array is : " )
for i in range ( 0 , 9 ):
print (arr1[i])


输出:

The occurrences of 1 in array is : 
2
The modified array is : 
1
2
3
1
2
5
1
2
3

6.fromlist(列表) :-此函数用于 附加一个列表 在其论点中提到 到数组末尾 .

7.托利斯 :-此函数用于 将数组转换为列表 .

# Python code to demonstrate the working of
# fromlist() and tolist()
# 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 ])
# initializing list
li = [ 1 , 2 , 3 ]
# using fromlist() to append list at end of array
arr.fromlist(li)
# printing the modified array
print ( "The modified array is : " ,end = "")
for i in range ( 0 , 9 ):
print (arr[i],end = " " )
# using tolist() to convert array into list
li2 = arr.tolist()
print ( "
"
)
# printing the new list
print ( "The new list created is : " ,end = "")
for i in range ( 0 , len (li2)):
print (li2[i],end = " " )


输出:

The modified array is : 1 2 3 1 2 5 1 2 3 
The new list created is : 1 2 3 1 2 5 1 2 3

本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

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