① 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