【问题标题】:Find if an element is between 2 other elements of a list?查找一个元素是否在列表的其他两个元素之间?
【发布时间】:2020-04-20 20:10:09
【问题描述】:

在我的一个程序中,我需要确定一个元素是否在另外两个元素之间才能找到价格。 例如,对于屠夫,我有一份订单、数量和价格的清单。 数量[5,10,15,20,25] 价格[3,2.9,2.7,2.4,2.1] 例如,如果我有 7 的新订单,我想插入数量为 5 和 10 的价格并得到 2.95(3+2.9/2) 我的问题不是计算价格,而是确定一个元素是否在我的列表中的其他 2 个元素之间的第一步。

提前致谢。

【问题讨论】:

  • 列表是否总是按照数量的升序排列?
  • 是的,它总是按升序排列。

标签: python list if-statement


【解决方案1】:

快速的方法是利用Python binary search module的二分法

二分查找的复杂度为 O(log N),其中 N = len(quantity)

代码

from bisect import bisect_left 

def binary_search(a, x):
    if x <= a[0] or x >= a[-1]:
        return None            # outside min/max range of a

    i = bisect_left(a, x)

    return None if a[i] == x else i # a[i] == x means on bin (so not between)

用法

q = [5,10,15,20,25]

for v in range(4, 26, 1):
    answer = binary_search(q, v)
    if answer:
        print('value {} between bins {} and {}'.format(v, answer, answer+1))
    else:
        print('value {} not between bins'.format(v))

出局

value 4 not between bins
value 5 not between bins
value 6 between bins 1 and 2
value 7 between bins 1 and 2
value 8 between bins 1 and 2
value 9 between bins 1 and 2
value 10 not between bins
value 11 between bins 2 and 3
value 12 between bins 2 and 3
value 13 between bins 2 and 3
value 14 between bins 2 and 3
value 15 not between bins
value 16 between bins 3 and 4
value 17 between bins 3 and 4
value 18 between bins 3 and 4
value 19 between bins 3 and 4
value 20 not between bins
value 21 between bins 4 and 5
value 22 between bins 4 and 5
value 23 between bins 4 and 5
value 24 between bins 4 and 5
value 25 not between bins

【讨论】:

    【解决方案2】:

    一种方法可能是:

    如果列表保持顺序(总是升序)并且两个列表中有相同数量的项目,您可以将第一个列表转换为对(元组)并比较新数字 (7) 如果它大于第一个元组的元素小于第二个。

    data = [5,10,15,20,25]
    price= [3,2.9,2.7,2.4,2.1]
    
    tuple_list = [(x, y) for x, y in zip(data, data[1:])]
    # [(5, 10), (10, 15), (15, 20), (20, 25)]
    

    然后您可以使用元组的索引进行操作: (但您可以随意更改)

    sum_to_calc = 0
    new_number = 7
    
    for x in tuple_list:
     if x[0] < new_number and x[1] > new_number:
       for index, y in enumerate(data):
         if x[0] == y or x[1] == y :
            sum_to_calc += price[index]
    
    print(sum_to_calc/2)
    

    【讨论】:

      【解决方案3】:

      这是一个函数,它表达了按照您的要求做事的一种方式。但是,您必须在运行之前将数字附加到列表中:

      def is_between(lst, val):
          i = 0 # Mark number of iterations
          for lt in lst:
              if lt==val: # If the current value in the list is the one you're looking for
                  try:
                      assert i!=0 # Check to see if there are any values on either side of it
                      lst[i+1]
                  except:
                      return False
                  return True
              i += 1
      

      【讨论】:

        【解决方案4】:

        我的解决方案假设数量列表始终按升序排列,并且与价目表具有相同的长度。

        quantity = [5, 10, 15, 20, 25]
        price = [3, 2.9, 2.7, 2.4, 2.1]
        
        # Could be quantity or price
        list_length = len(quantity)
        
        subtotal = 0
        
        # Number you want to check
        number = 7
        
        for x in range(list_length):
            if x+1 == list_length:
                break
        
            if quantity[x] <= number and number <= quantity[i+1]:
                subtotal += price[x] + price[x+1]
                break
        
        if subtotal:
            total = 2.95 * (subtotal / 2)
        else:
            # Do something
            pass
        
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-03-18
          • 2020-07-07
          • 2021-01-01
          • 2023-01-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多