编写一个 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))
寻找元组中最大项目的 Python 程序。
我们使用元组排序函数(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