编写一个 Python 程序来查找最小或最大的集合项。在此,我们使用 Set max 函数来打印最小的集合项。
# Set Min Item
smtSet = {98, 11, 44, 32, 7, 19, 65, 80, 91}
print("Set Items = ", smtSet)
print("Smallest Item in mxSet Set = ", min(smtSet))
Python 集合最小数字输出
Set Items = {32, 65, 98, 7, 11, 44, 80, 19, 91}
Smallest Item in mxSet Set = 7
Python 程序查找集合中的最小项
此 Python 示例允许输入集合项。接下来,我们使用 Set sorted 函数 (sorted(smtSet)) 按升序对集合进行排序。接下来,我们打印第一个索引位置的项。
# Set Min Item
smtSet = 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))
smtSet.add(value)
print("Set Items = ", smtSet)
sortVals = sorted(smtSet)
print("Smallest Item in smtSet Set = ", sortVals[0])
print("Data Type of sortVals = ", type(sortVals))

Python 程序使用函数打印最小集合数
If 语句 (if setSmallest > i) 检查 setSmallest 值是否大于任何集合项。如果为 True,则将该项分配给最小的 集合 项。
# Set Min Item
def SetSmallest(smtSet, setSmallest):
for i in smtSet:
if(setSmallest > i):
setSmallest = i
return setSmallest
smtSet = 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))
smtSet.add(value)
setSmallest = value
print("Set Items = ", smtSet)
lar = SetSmallest(smtSet, setSmallest)
print("Smallest Item in smtSet Set = ", lar)
Python 集合最小值输出
Enter the Total Set Items = 5
Enter the 1 Set Item = 11
Enter the 2 Set Item = 65
Enter the 3 Set Item = 56
Enter the 4 Set Item = 2
Enter the 5 Set Item = 88
Set Items = {65, 2, 11, 56, 88}
Smallest Item in smtSet Set = 2