最大值的下标
winner = np.argmax(scores)
多个最大值的下标
(np.argwhere返回数组中非0元素的索引)
winners = np.argwhere(scores == np.amax(scores))
winners = winners.flatten()
数组排序并返回原下标
lst = np.array([0.2,0.3,0.1,0.4,0.5])
import numpy as np
lst_sorted = np.sort(lst) # small to large
seq = np.argsort(lst) # lst[seq[i]] ranks i (seq[0] indicates the smallest element)
rank = np.argsort(seq)+1 # score of sorting, the lst[i] ranks rank[i] (rank[i]=0 indicates the samllest element is lst[i])
'''another method'''
from operator import itemgetter
seq, lst_sorted = zip(*sorted(enumerate(lst), key=itemgetter(1)))
元素逐个求平方
err=[num*num for num in Lst]
创建空list矩阵
Lst= [[] for _ in range(K)]
Lst= lsts = [[[] for _ in range(b)] for _ in range(a)]
列表中不重复元素的个数
a = ['lunch', 'afternoon', 'lunch', 'dinner', 'dinner', 'dawn', 'lunch']
print(len(set(a))) # 4
axis=0代表行向,=1代表列向
a = [[1,2],[3,4]]
print(np.sum(a,axis=0))
[4 6]
print(np.sum(a,axis=1))
[3 7]
倒序输出
idx = np.arange(5)
print(idx)
print(idx[::-1])