python补充学习 np.unique namedtuple

2020-09-15  本文已影响0人  锦绣拾年

np.unique

http://codingdict.com/article/21579
此函数返回输入数组中的唯一元素数组。该函数可以返回唯一值数组的数组和关联索引数组。索引的性质取决于函数调用中返回参数的类型。

numpy.unique(arr, return_index, return_inverse, return_counts)
这里

序号 参数和描述
1 arr 输入数组。如果不是一维阵列,将会变平
2 return_index 如果为True,则返回输入数组中元素的索引
3 return_inverse 如果为True,则返回唯一数组的索引,该索引可用于重建输入数组
4 return_counts 如果为True,则返回唯一数组中的元素出现在原始数组中的次数

https://blog.csdn.net/yangyuwen_yang/article/details/79193770
一个参数,去重

import numpy as np
A = [1, 2, 2, 5,3, 4, 3]
a = np.unique(A)
B= (1, 2, 2,5, 3, 4, 3)
b= np.unique(B)
C= ['fgfh','asd','fgfh','asdfds','wrh']
c= np.unique(C)
print(a)
print(b)
print(c)
#   输出为 [1 2 3 4 5]
# [1 2 3 4 5]
# ['asd' 'asdfds' 'fgfh' 'wrh']

多个参数,返回多个参数代表的方法的结果。

a, s= np.unique(A, return_index=True)
print(a)
print(s)
# 运行结果
# [1 2 3 4 5]
# [0 1 4 5 3]
a, s,p = np.unique(A, return_index=True, return_inverse=True)
print(a)
print(s)
print(p)
# 运行结果
# [1 2 3 4 5]
# [0 1 4 5 3]
# [0 1 1 4 2 3 2]

namedtuple

https://blog.csdn.net/qq_30159015/article/details/80356226

上一篇 下一篇

猜你喜欢

热点阅读