python去除数组重复项_Python中数组的通用数据项和方法

python去除数组重复项

List of common and most useful data items and methods of an array module in python

python数组模块的常见和最有用的数据项和方法列表

Sr NoData Item/Method Description Example (Consider a is array name)
1array.typecodeReturns the typecode of the array a.typecode
2array.itemssizeReturns the item size of an element of the array a.itemsize
3array.append(x)Appends x at the end of the array a.append(100)
4array.count(x)Returns the occurrence of element x in the array a.count(10)
5array.index(x)Returns the index of x in the arraya.index(10)
6array.insert(i,x)Inserts the element x at given index i in the array a.insert(2,10)
7array.pop(i)Removes the item from the index i and returns the elementa.pop(2)
8array.remove(x)Removes the first occurrence of element x in the arraya.remove(20)
9array.reverse()Reverses the elements of the array a.reverse()
10array.tolist()Converts array to a list with same itemsa.tolist()
高级 数据项/方法 描述 示例(考虑a为数组名称)
1个 array.typecode 返回数组的类型代码 类型码
2 array.itemssize 返回数组元素的项目大小 项目大小
3 array.append(x) 在数组末尾附加x 追加(100)
4 array.count(x) 返回元素x在数组中的出现 计数(10)
5 array.index(x) 返回数组中x的索引 索引(10)
6 array.insert(i,x) 在数组中给定索引i处插入元素x 插入(2,10)
7 array.pop(i) 从索引i移除项目并返回元素 流行音乐(2)
8 array.remove(x) 删除元素x在数组中的第一次出现 a。删除(20)
9 array.reverse() 反转数组的元素 a.reverse()
10 array.tolist() 将数组转换为具有相同项目的列表 a.tolist()

Python program demonstrating use of all above-written data items and methods

演示使用所有上面编写的数据项和方法的Python程序

# imporating array module to use array 
import array as arr


# declare and initialze array of type signed int
a = arr.array ("i", [10, 20, -30, 40, -50] )

# typecode
print ("Typecode of a: ", a.typecode)

# itemsize
print ("Item size of an element: ", a.itemsize);

# append new item at the end of the array 
a.append(60)
print ("Elements: ", a);

# array count(x)
print (a.count(20))

# index (X)
print ("Index: ", a.index(20))

# insert (i,x)
a.insert (2,99)
print ("Elements: ", a);

# pop (i)
print (a.pop(2), " is removed...")
print ("Elements: ", a);

# remove (x)
a.remove(-30)
print ("Elements: ", a);

# reverse
a.reverse ()
print ("Elements: ", a);

# tolist
print ("Elements (tolist): ", a.tolist ())
print ("Type of a: ", type (a))

Output

输出量

Typecode of a:  i
Item size of an element:  4
Elements:  array('i', [10, 20, -30, 40, -50, 60])
1
Index:  1
Elements:  array('i', [10, 20, 99, -30, 40, -50, 60])
99  is removed...
Elements:  array('i', [10, 20, -30, 40, -50, 60])
Elements:  array('i', [10, 20, 40, -50, 60])
Elements:  array('i', [60, -50, 40, 20, 10])
Elements (tolist):  [60, -50, 40, 20, 10]
Type of a:  <class 'array.array'>


翻译自: https://www.includehelp.com/python/common-data-items-and-methods-of-an-array-in-python.aspx

python去除数组重复项

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值