如何在python中存储一系列值

时间:2016-06-27 23:26:26

标签: python excel store

我刚刚开始使用python几天前,我在寻找解决方案方面遇到了一些问题。

所以,我一直在使用pycharm并且我已经下载了一些模块,比如 xlrd,xlwings和xlwt 来分析和格式化我的Excel数据。

我的主要目标是从列中找到最高值作为正值和负值,例如:

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

感谢您的耐心和抱歉语法错误。

2 个答案:

答案 0 :(得分:1)

您可以使用max(some_List)功能。它返回列表中的最大值。

以同样的方式,您可以使用min(some_List)功能。它返回列表的最小值。

我不知道任何返回列表的2个最大值和2个最小值的函数。你可能必须自己构建它。

至于在列表中存储最大值和最小值,您可以执行以下操作:

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.

我希望这会对你有所帮助。你能澄清一下你想做什么吗?在什么情况下你想要做到这一点?

答案 1 :(得分:0)

猜测工具python允许你:

  1. sorted(l)获取与您类似的列表并按降序返回新列表,即最大值为索引0或l[0]

  2. max(l),返回最大值

  3. min(l)返回最小值

  4. 例如,为了将列表中的最高n个数字添加到新列表中,您只需执行以下操作:new_list = sorted(l)[0:n]