python数组处理:最值与下标

最大值的下标

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])
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值