① python 数组求最大值
print dict([(k,v) for k,v in d.items() if v==max(d.values())])
② python求二维数组中最大值的位置
使用numpy的max函数,该函数也是适用于其他维度的数组。
例子如下:
>>> a = np.arange(4).reshape((2,2))
>>> a
array([[0, 1],
[2, 3]])
>>> np.amax(a) # 整个数组的最大值
3
>>> np.amax(a, axis=0) # 沿第一个轴的最大值
array([2, 3])
>>> np.amax(a, axis=1) # 沿第二个轴的最大值
array([1, 3])
③ python数组有多个最大值,寻找最后一个最大值
用np.where函数或者用列表表达式就可以满足你的要求(见图)
④ python数组找最大值
max(d.items(),key=lambda x:x[1])
⑤ python找数组中的最大值
enumerate()函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标。
x = [3, 2.2, 7.4, 6, 4]
list(enumerate(x))
# 输出 [(0, 3), (1, 2.2), (2, 7.4), (3, 6), (4, 4)]
operator.itemgetter()函数用于获取对象的哪些维的数据,参数为想要取的一些维度序号。
x = [3, 2.2, 7.4, 6, 4]
b1 = operator.itemgetter(2, 1)
b1(x) # 输出 (7.4, 2.2)
b2 = operator.itemgetter(3)
b2(x) # 输出 6
max()函数有一个应用很巧妙的参数key,在这里定义为operator.itemgetter(1),表示对enumerate(x)每个元素的第一维做比较(从0维开始),然后返回第一维值最大的元素,即包含索引和数值。
key参数还有其他的巧妙应用:
# 获取数组中绝对值最大的数
x = [3, 2.2, -7.4, 6, 4]
max(x, key=abs) # 返回 -7.4
# lambda表达式形式
x = [3, 2.2, -7.4, 6, 4]
max(x, key=lambda x: abs(x)) # 返回 -7.
⑥ python求数组中最大值
求可迭代对象(列表、元组、集合等都是)的最大值可以用内置的max函数。
如:
print(max([1,3,5,99,2]))
print(max((1,3,5,99,2)))
print(max({1,3,5,99,2}))
输出都是99。
⑦ 用python 求一个数组中最大的三个元素及其所在位置
参考代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FindMaxWithIndex
{
/// <summary>
/// 有一个数组,每个元素的值都是实数,请写出求最大元素的值及其位置的算法
/// </summary>
class Program
{
static void Main(string[] args)
{
double[] Num = new[] { -8, 4543.9, 4543.9, 3, 45, 654.7, 7, 66, 35, 45, 4, 6, 4543.9, 5, 46, 54, 6, 43, 5.980, 34, 4543.9 };
//double[] Num = new [] { 1.0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
int[] index = SearchMaxWithIndex(Num);
Console.WriteLine("The max number is: {0}", Num[index[0]]);
Console.Write("The index of max number is:");
for (int i = 0; i < index.Length; i++)
{
if (index[i] == -1) break;
Console.Write(" '{0}'", index[i]);
}
Console.ReadKey();
}
private static int[] SearchMaxWithIndex(double[] arr)
{
int[] pos = new int[arr.Length]; //记录最大值所在位置的数组
int position = 0; //初始设定数组的第1个元素为最大值
int j = 1;//j指示位置数组pos的下标
for (int i = 1; i < arr.Length; i++)
{
if (arr[i] > arr[position])
{
position = i; //记下新的最大值的位置
j = 1; //位置数组pos的下标恢复为1,下标为0的位置为position预留
}
else if (arr[i] == arr[position])
pos[j++] = i; //记下重复最大值的位置
}
pos[0] = position; //位置数组pos的下标为0的位置为position预留
if (j < arr.Length) pos[j] = -1; //-1为标识值,表示位置数组pos下标为0, 1, 2…(j-1)的位置存放的是最大值所在的位置
return pos;
}
}
}
⑧ python求一组数组最大值,最小值,平均值
Python的数组就是列表。比如对列表ls=[1,2,3,4,5,6]来处理。
sum(ls)#返回列表总和
max(ls)#返回列表里最大值
min(ls)#返回列表里最小值
len(ls)#返回列表长度
sum(ls)/len(ls)#返回列表的平均值
(sum(ls)-max(ls)-min(ls))/(len(ls)-2)#返回比赛评分常用的规则,去掉一个最高分,去掉一个最低分,再求平均分。
⑨ python中如何取一列数最大值
如果是从列表中找最大值,则可以使用max(),如:
In[279]:a=range(10)
In[280]:max(a)
Out[280]:9
如果是从数组找最大值,则可以使用numpy.max()函数,如:
In[281]:a=np.arange(10)
In[282]:a.max()
Out[282]:9
如果是一个二维数组,取某一列的最大值,则:
In[285]:a=np.arange(12).reshape(3,4)
In[286]:a
Out[286]:
array([[0,1,2,3],
[4,5,6,7],
[8,9,10,11]])
In[287]:a[2,:].max()
Out[287]:11