clwn.net
当前位置:首页 >> python取最大值 >>

python取最大值

如果是list,有max(list) 也可以自己写排序算法,比如冒泡排序 a=[3,4,2,6,3]for i in range(0,len(a)): for j in range(i+1,len(a)): first=int(a[i]) second=int(a[j]) if first

def max3(a,b,c): maxnum=a if a>b else b if c>maxnum: maxnum=c return maxnum if __name__=='__main__': print(max3(3,2,1) 2. def max(a,b,c): if a>b: g=b b=a a=g if a>c: g=c c=a a=g if b>c: g=b b=c c=g print 'a=',a,'\tb=',b,'\tc=',...

c = [-10,-5,0,5,3,10,15,-20,25] # 返回最小值 >>> n=c.index(min(c)) >>> n 7 >>> c[7] -20 # 返回最大值 print c.index(max(c))

c = [-10,-5,0,5,3,10,15,-20,25] # 返回最小值 >>> n=c.index(min(c)) >>> n 7 >>> c[7] -20 # 返回最大值 print c.index(max(c))

python的内建函数就有求最大最小值的函数。 求最大值:max() 求最小值:min() 求和:sum() 他们的第一个参数都是可遍历对象,也就是说可以是字符串,tuple或者list,其它参数请参照文档

program help; var i,n,min,max,k:longint; begin min:=maxlongint; max:=0; read(n); for i:=1 to n do begin read(k); if k>max then max:=k; if k

#/usr/bin/env python3#coding=utf-8import res = input('请输入10个整数以空格隔开: ')nums = [int(i) for i in re.findall(r'\d+', s)]zuida, zuixiao = max(nums), min(nums)print('最大值是{},位于第{}'.format(zuida, nums.index(zuida) +...

>>> c=[-10,-5,0,5,3,10,15,-20,25] >>> a=c.index(max(c)) >>> b=max(c) >>> c[a]=c[0] >>> c[0]=b >>> print c [25, -5, 0, 5, 3, 10, 15, -20, -10]

def max3(a,b,c): maxnum=a if a>b else b if c>maxnum: maxnum=c return maxnum if __name__=='__main__': print(max3(3,2,1) 2. def max(a,b,c): if a>b: g=b b=a a=g if a>c: g=c c=a a=g if b>c: g=b b=c c=g print 'a=',a,'\tb=',b,'\tc=',...

N=int(input('Enter the lenth of list:')) list=[] while N: (tab)num=input('Enter a number:') (tab)list.append(num) (tab)N=N-1 print(max(list))

网站首页 | 网站地图
All rights reserved Powered by www.clwn.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com