python列表最大值
㈠ python列表求最大值
>>>a=[[4,2,5,7,6,3],[5,8,3,4,9,0]]
>>>b=[[each.index(max(each))+1,max(each)]foreachina]
>>>b
[[4,7],[5,9]]
>>>
㈡ python怎么知道列表中最大的元素是第几个
l = [1,2,3]
maxnum = max(l)
print(l.index(maxnum))
index函数只会返回列表里第一个匹配的值,如果最大值在列表里有多个,则无法全部查询到
一个冒泡排序的思路,逐一对比,并记住当前最大值的下标,可以得到最大值的多个下标
l = [1,2,3,0,3]
indeiesDict = {}
maxnum = 0
for i in range(len(l)):
if i > 0 :
if l[i] >= l[i-1]:
maxnum = l[i]
index = i
else:
maxnum = l[i-1]
index = i-1
if indeiesDict.get(maxnum):
indeiesDict[maxnum].add(index)
else:
indeiesDict[maxnum] = set([index])
print(maxnum,indeiesDict[maxnum])
㈢ python找出最大数
python找出几个数中最大值的方法:
1、简单的使用if-else进行判断
List = [12, 34, 2, 0, -1]
Max = List[0] # 定义变量Max用来存储最大值,初始值赋值为列表中任意一个值
for i in List:
if i > Max:
Max = i
print("这个列表中最大值为:", Max)
2、使用max函数来完成
List = [1, 34, 5, 6, 98]
Max = max(List)
print("这个列表中最大值为:", Max)
max() 方法返回给定参数的最大值,参数可以为序列。
以下是 max() 方法的语法:
max( x, y, z, .... )
x -- 数值表达式。
y -- 数值表达式。
z -- 数值表达式。
㈣ 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
㈤ python 求最大值
####求10个数据的最大值########
list=[]
for i in range(10):#这里可以设置数据的多少
list.append(float(input("请输入数据"))) #输入数据,如果都是整数可以把float改为int
max=list[0]
for i in range(10):#这里数据与上面的for里面的保持一致
if list[i]>max:#如果数据比max大就会更新max
max=list[i]
print("最大值为:%f"%max)#输出
#望采纳
㈥ python如何求列表最大值
Python 的内置函数具有查找极值的功能。Max () find the maximum: max () find the minimum: min () find the sum: sum ()他们的第一个参数是可遍历的对象,这意味着它们可以是字符串、元组或列表
㈦ python 最大值在第几行
进行python编程的时候,想查找列表的最大值和最小值,怎么查找,下面来介绍一下方法。
设备:联想电脑
系统:win10
软件:python版本3.5.2
1、首先在py文件中,输入a=[1,3,5,7,4,2],创建一个a列表。
㈧ 用python输出最大的数和最小的数,及最大数和最小数的平均值,这个应该怎么做
numbers=[1,2.1,1.0,3.11,5.2,6.6,7,8,9,10,10.0]
#定义一个存放最小数的数组
min_numbers=[]
#定义一个存放最大数的数组
max_numbers=[]
#使用max()、min()函数求取列表最大值和最小值,并输出
min_number=min(numbers)
max_number=max(numbers)
print("数组中的最小数是:",min_number)
print("数组中的最大数是:",max_number)
i=0
fornumberinnumbers:
i+=1
#当遍历到最小值时
ifnumber==min_number:
min_numbers.append(i)
#当遍历到最大值时
elifnumber==max_number:
max_numbers.append(i)
print("最小数在数组中的顺序是:",min_numbers)
print("最大数在数组中的顺序是:",max_numbers)
建议实操实验一下,研究其中的逻辑,python基础知识的时候看到的有返回列表最大元素的函数和返回列表最小元素的函数,这一点很好的解决在在数组中寻找到最大数和最小数问题。我定义一个变量i=0,让每次遍历后i=i+1,这样当遍历输出的元素等于最大值和最小值是i值恰好是最大值 和最小值在数组中的位置。
希望这个回答可以帮助到你。
㈨ 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)#返回比赛评分常用的规则,去掉一个最高分,去掉一个最低分,再求平均分。