【问题标题】:How to store series of values in python如何在python中存储一系列值
【发布时间】:2016-10-30 02:31:39
【问题描述】:

几天前我刚开始使用 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)存储在列表或其他东西中??

感谢您的耐心等待,对于语法错误,我们深表歉意。

【问题讨论】:

  • 一个简单的解决方案,srt = sorted(a)(mx2, mx1), mn = str[-2:], srt[0],确保您已将值转换为浮点数

标签: python excel store


【解决方案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.

我希望这对你有帮助。你能澄清一下你想要做什么吗?您在什么情况下尝试这样做?

【讨论】:

    【解决方案2】:

    推测python允许您使用的工具:

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

    2. max(l)返回最大值

    3. min(l) 返回最小值

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-02
      • 1970-01-01
      • 2011-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-06
      相关资源
      最近更新 更多