您的位置:

Python 程序:寻找最小集合项目

编写一个 Python 程序来查找最小或最大集合项。这里,我们使用设置最大值功能来打印最小设置项目。

# 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 示例允许输入设置的项目。接下来,我们使用集合排序函数(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 语句(如果 setSmallest > i)检查 set minist 值是否大于任何设置项。如果为真,则将该项目分配给最小设置项目。

# 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)

蟒组最小物品输出

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