編寫一個 Python 程序來查找最大或最大集合項。這裡,我們使用設置最大值功能來打印最大的設置項目。
# Set Max Item
mxSet = {25, 67, 36, 98, 11, 32, 19, 80, 91}
print("Set Items = ", mxSet)
print("Largest Item in mxSet Set = ", max(mxSet))
Python 最大集數輸出
Set Items = {32, 98, 67, 36, 11, 80, 19, 25, 91}
Largest Item in mxSet Set = 98
尋找集合中最大項目的 Python 程序
這個 Python 示例允許輸入設置的項目。接下來,我們使用集合排序函數(sorted(mxSet))對集合進行升序排序。接下來,我們打印最後一個索引位置項。我們可以使用索引值,因為排序函數返回一個列表。
# Set Max Item
mxSet = set()
number = int(input("Enter the Total Set Items = "))
for i in range(1, number + 1):
value = int(input("Enter the %d Set Item = " %i))
mxSet.add(value)
print("Set Items = ", mxSet)
sortedVal = sorted(mxSet)
print("Largest Item in mxSet Set = ", sortedVal[len(mxSet) - 1])
print("Data Type of sortedVal = ", type(sortedVal))
在這個 Python 程序中,我們創建了一個最大集合函數來打印最大集合數。if 語句(If(設置最大值< i)) checks whether the setLargest value is less than any of the set items. If True, assign that item as the largest 設置項。
# Set Max Item
def SetLargest(mxSet, setLargest):
for i in mxSet:
if(setLargest < i):
setLargest = i
return setLargest
mxSet = set()
number = int(input("Enter the Total Set Items = "))
for i in range(1, number + 1):
value = int(input("Enter the %d Set Item = " %i))
mxSet.add(value)
setLargest = value
print("Set Items = ", mxSet)
lar = SetLargest(mxSet, setLargest)
print("Largest Item in mxSet Set = ", lar)
巨蟒最大設定物品輸出
Enter the Total Set Items = 4
Enter the 1 Set Item = 99
Enter the 2 Set Item = 122
Enter the 3 Set Item = 33
Enter the 4 Set Item = 100
Set Items = {33, 122, 99, 100}
Largest Item in mxSet Set = 122
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/288971.html