如何在Python中存储一系列值
我刚刚开始使用python,而且我在寻找解决scheme时遇到了一些问题。
所以,我一直在使用pycharm,我已经下载了一些模块,如xlrd,xlwings和xlwt来分析和格式化我的excel数据。
我的主要目标是从一列中find最高值,作为正值和负值,例如:
a = [0, 0.34, 0.7, 0.88, 0.98, 0.5, 0.3, 0.1, -0.1, -0.4, -0.6,-0.9, -0.2, 0, 0.1, 0.5, 0.9, 0.3,0.1, 0]
所以我的问题是:无论如何存储的最大值(0.98和0.9)和最小值(-0.9)列表或东西?
感谢您的耐心和歉意的语法错误。
你可以使用max(some_List)
函数。 它返回列表中的最大值。
以同样的方式,你可以使用min(some_List)
函数。 它返回一个列表的最小值。
我不知道有任何函数返回列表的2个最大值和2个最小值。 您可能必须自己构build。
至于将最大值和最小值存储在列表中,可以执行以下操作:
my_New_List = [] // declare a new list my_New_List[0] = max(a) // retreive the maximum value of your starting list and assign in to the index 0 of a new list. my_New_List[1] = min(a) //retreive the minimum value of your starting list and assign in to the index 1 of a new list.
我希望这可以帮助你。 你能澄清一下你想做什么吗? 在什么情况下,你正在试图做?
为了推测Python的工具,你可以:
-
sorted(l)
,它采用像你这样的列表,并按降序返回一个新列表,即最大值在索引0或l[0]
-
max(l)
返回最大值 -
min(l)
返回最小值
例如,为了从列表中获得最高的n
数字到一个新的列表,你只需要做: new_list = sorted(l)[0:n]