python去除数组重复项
List of common and most useful data items and methods of an array module in python
python数组模块的常见和最有用的数据项和方法列表
Sr No | Data Item/Method | Description | Example (Consider a is array name) |
---|---|---|---|
1 | array.typecode | Returns the typecode of the array | a.typecode |
2 | array.itemssize | Returns the item size of an element of the array | a.itemsize |
3 | array.append(x) | Appends x at the end of the array | a.append(100) |
4 | array.count(x) | Returns the occurrence of element x in the array | a.count(10) |
5 | array.index(x) | Returns the index of x in the array | a.index(10) |
6 | array.insert(i,x) | Inserts the element x at given index i in the array | a.insert(2,10) |
7 | array.pop(i) | Removes the item from the index i and returns the element | a.pop(2) |
8 | array.remove(x) | Removes the first occurrence of element x in the array | a.remove(20) |
9 | array.reverse() | Reverses the elements of the array | a.reverse() |
10 | array.tolist() | Converts array to a list with same items | a.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去除数组重复项