【问题标题】:Comparing Numbers in Multiple Lists Python比较多个列表 Python 中的数字
【发布时间】:2022-06-11 13:55:22
【问题描述】:

我有 5 组如下所示的值:

[[83.91649562 79.51353257]
 [87.57474691 84.66544614]
 [84.08067077 85.19063777]
 [86.97440657 86.20994142]
 [82.91694642 84.65734125]]

我的目标是将每组中的两个值与此标准进行比较:

  1. 在任何列表中,如果 item1 和 item2 >= 80 AND item1
  2. 在任何列表中,如果 item1 和 item2 为 item2,则返回 10
  3. 否则返回0

这就是我所做的

def myfunction(data):
    data = data.iloc[:, [0, 1]].values
    for x, y in enumerate(data):
        if (x-y).all() >= 80 and x < y:
            return -10
        else:
            return 0

现在我返回 0,但是第 3 和第 5 个列表符合条件并且应该返回 -10,所以我没有转到第二个 if 语句。 我还尝试使用以下方法设置数据:

data = data.iloc[:, [0, 1]].values.tolist()

将数据用作

[[83.91649561983937, 79.51353257164777], [87.57474691499445, 84.66544613660386], [84.08067077024245, 85.19063776835876], [86.97440656949847, 86.20994141824511], [82.91694641784167, 84.65734125252753]]

没有运气。我一直在使用 enumarate() ,因为我在没有收到错误消息方面取得了最大的成功,但我不确定这是否是我解决这个问题所需要的。

谢谢大家!

【问题讨论】:

    标签: python list dataframe loops


    【解决方案1】:

    不使用任何库,为简化起见,您可以像这样编写循环

    #Devil
    lst = [[83.91649562, 79.51353257],
        [87.57474691, 84.66544614],
        [84.08067077, 85.19063777],
        [86.97440657, 86.20994142],
        [82.91694642, 84.65734125]]
    
    fill_out = []
    for i in lst:
        #print(i)
        item1 = i[0] ; item2 = i[1]
        if item1 >= 80 and item2 >= 80 and item1 < item2:
            fill_out.append(-10)
        elif item1 <= 20 and item2 <= 20 and item1 > item2:
            fill_out.append(10)
        else:
            fill_out.append(0)
    
    print("output:",fill_out)
    #output : [0, 0, -10, 0, -10]
    

    【讨论】:

    • 感谢没有库的版本。我不确定如何隔离 item[1] 和 item[2]。我想因为我使用的是枚举,所以我不断收到一条错误消息,告诉我使用 any() 或 all()。这将帮助我完成其他功能?
    【解决方案2】:

    您可以尝试以下方法:

    import numpy as np
    
    a = np.array([[83.91649561983937, 79.51353257164777],
                  [87.57474691499445, 84.66544613660386],
                  [84.08067077024245, 85.19063776835876],
                  [86.97440656949847, 86.20994141824511],
                  [82.91694641784167, 84.65734125252753]])
    
    conds = [(np.diff(a) > 0).ravel() & np.all(a >= 80, axis=1),
             (np.diff(a) < 0).ravel() & np.all(a <= 20, axis=1)]
    np.select(conds, [-10, 10])
    

    它给出:

    array([  0,   0, -10,   0, -10])
    

    【讨论】:

      【解决方案3】:
      import pandas as pd
      
      
      def myfunction(data):
          where = (data[0] >= 80) & (data[1] >= 80) & (data[0] < data[1])
          if len(data.loc[where]) > 0:
              return -10 
      
          where = (data[0] <= 20) & (data[1] <= 20) & (data[0] > data[1])
          if len(data.loc[where]) > 0:
              return 10
      
          return 0
      
      
      data = pd.DataFrame([
          [83.91649562, 79.51353257],
          [87.57474691, 84.66544614],
          [84.08067077, 85.19063777],
          [86.97440657, 86.20994142],
          [82.91694642, 84.65734125],
      ])
      
      myfunction(data)
      

      根据初始代码(.iloc[], .any())制作这个函数,假设OP使用pandas。

      输出:-10

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 2019-04-04
      • 2016-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-19
      • 1970-01-01
      相关资源
      最近更新 更多