Python Set中的最大值和最小值

2021年5月1日17:31:27 发表评论 2,931 次浏览

在本文中, 我们将学习如何使用Python的内置函数在Python集合中获取最大和最小元素。

例子:

Input : set = ([8, 16, 24, 1, 25, 3, 10, 65, 55])
Output : max is 65

Input : set = ([4, 12, 10, 9, 4, 13])
Output : min is 4

集合中的max()

Python中的内置函数max()用于获取集合中所有元素的最大值。

# Python code to get the maximum element from a set
def MAX (sets):
     return ( max (sets))
      
# Driver Code
sets = set ([ 8 , 16 , 24 , 1 , 25 , 3 , 10 , 65 , 55 ])
print ( MAX (sets))

输出如下:

65

集合中的min()

# Python code to get the minimum element from a set
def MIN (sets):
     return ( min (sets))
      
# Driver Code
sets = set ([ 4 , 12 , 10 , 9 , 4 , 13 ])
print ( MIN (sets))

输出如下:

4

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: