创新互联Python教程:
贡嘎ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联建站的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!
编写一个 Python 程序来查找元组中最大或最大的项。这里,我们使用 Tuple max 函数返回最大的 Tuple 项。
# Tuple Max Item
mxTuple = (3, 78, 67, 34, 88, 11, 23, 19)
print("Tuple Items = ", mxTuple)
print("Largest Item in mxTuple Tuple = ", max(mxTuple))
我们使用元组排序函数(sorted(mxTuple))以升序对元组进行排序。接下来,我们打印最后一个索引位置项,它是最大的。
# Tuple Max Item
mxTuple = (2, 14, 11, 19, 25, 17, 33, 22, 30)
print("Tuple Items = ", mxTuple)
mxTuple = sorted(mxTuple)
print("Largest Item in mxTuple Tuple = ", mxTuple[len(mxTuple) - 1])
Tuple Items = (2, 14, 11, 19, 25, 17, 33, 22, 30)
Largest Item in mxTuple Tuple = 33
在这个 Python 示例中,我们将 Tuple 第一个值指定为最大值(Tuple maximum = mxTuple[0]),for 循环范围从 1 开始,遍历到 mxTuple 长度。if 条件(if(最大值< mxTuple[I])) examines whether the current 元组项大于最大值。如果为真,则将该元组值赋给最大值,并将索引值赋给最大值变量。
# Tuple Max Item
mxTuple = (25, 17, 19, 33, 55, 22, 40, 30, 50, 29)
print("Tuple Items = ", mxTuple)
tupLargest = mxTuple[0]
for i in range(1, len(mxTuple)):
if(tupLargest < mxTuple[i]):
tupLargest = mxTuple[i]
tupLargestPos = i
print("Largest Item in mxTuple Tuple = ", tupLargest)
print("Largest Tuple Item index Position = ", tupLargestPos)
Tuple Items = (25, 17, 19, 33, 55, 22, 40, 30, 50, 29)
Largest Item in mxTuple Tuple = 55
Largest Tuple Item index Position = 4
使用函数打印最大元组数的 Python 程序。
# Tuple Max Item
def tupleLargest(mxTuple):
tupLargest = mxTuple[0]
for i in mxTuple:
if(tupLargest < i):
tupLargest = i
return tupLargest
mxTuple = (19, 25, 77, 56, 89, 45, 55, 12)
print("Tuple Items = ", mxTuple)
lar = tupleLargest(mxTuple)
print("Largest Item in mxTuple Tuple = ", lar)
Tuple Items = (19, 25, 77, 56, 89, 45, 55, 12)
Largest Item in mxTuple Tuple = 89
本文名称:Python程序:寻找元组中最大项
网站网址:http://www.shufengxianlan.com/qtweb/news14/484914.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联