簡體   English   中英

列表 2 中的哪些數字比列表 1 中的每個數字更大和更小

[英]which numbers in list 2 are bigger and smaller than each number in list 1

我正在使用蟒蛇。 我有兩個列表,列表 1 是 7000 個整數,列表 2 是 25000 個整數。 我想遍歷列表 1 中的每個數字,找到列表 2 中比列表 1 中每個數字大的最接近的數字和最接近的比列表 1 中每個數字小的數字,然后計算列表 2 中這兩個數字之間的差值。到目前為止我有:

for i in list1:
    for j in list 2:
        if list2[j]<list1[i]:
            a = max(list2)
        elif list2[j]>list1[i]:
            b = min(list2)
            interval = b-a

這似乎不起作用。 我想找到列表 2 中小於列表 1 中特定數字的顯式數字並知道最大值,然后找出列表 2 中大於列表 1 中數字的最小數字。 有沒有人有任何想法? 謝謝

您可以使用bisect模塊,最壞情況復雜度O(N * logN)

import bisect
lis1 = [4, 20, 26, 27, 30, 53, 57, 76, 89, 101]
lis2 = [17, 21, 40, 49, 53, 53, 53, 53, 70, 80, 81, 95, 99] #this must be sorted
#use lis2.sort() in case lis2 is not sorted
for x in lis1:
       #returns the index where x can be placed in lis2, keeping lis2 sorted
       ind=bisect.bisect(lis2,x) 
       if not (x >= lis2[-1] or x <= lis2[0]):
           sm, bi = lis2[ind-1], lis2[ind]

           if sm == x:  
               """ To handle the case when an item present in lis1 is 
               repeated multiple times in lis2, for eg 53 in this case"""
               ind -= 1
               while lis2[ind] == x:
                   ind -= 1
               sm = lis2[ind]

           print "{} <= {} <= {}".format(sm ,x, bi)

輸出:

17 <= 20 <= 21
21 <= 26 <= 40
21 <= 27 <= 40
21 <= 30 <= 40
49 <= 53 <= 70
53 <= 57 <= 70
70 <= 76 <= 80
81 <= 89 <= 95

盡管這不會為4101輸出任何內容,因為 4 小於 lis2 中的任何元素,而 101 大於 lis2 中的任何元素。 但如果需要,可以修復。

這是使用 NumPy 的矢量化解決方案。 它應該非常快,因為它在 Python 中沒有循環(除了最后的打印階段)。

import numpy as np

# set up fake data
l1 = np.array([1.9, 2, 2.1]) # or whatever list you have
l2 = np.array([1, 2, 5, 10]) # as above
l2.sort() # remove this line if it's always sorted

# the actual algorithm
indexes = np.searchsorted(l2, l1, side='right')
lower = l2[indexes - 1]
upper = l2[indexes]
diffs = upper - lower

# print results for debugging
for value, diff in zip(l1, diffs):
    print "value", value, "gap", diff

這是帶有上述硬編碼測試數據的輸出:

value 1.9 gap 1
value 2.0 gap 3
value 2.1 gap 3

首先,您的示例不是有效代碼,或者至少它沒有做您想要它做的事情。 如果你有

for i in list1:

那么 i 不是索引,而是 list1 的一個元素。 所以首先你要比較 i 和 j,而不是 list[i] 和 list[j]。

使用列表推導式應該更容易>

for i in list1:
    a = max([n for n in list2 if n < i])
    b = min([n for n in list2 if n > i])

您可能需要添加一個或兩個 if 以確保 a 和 b 存在,但它應該像這樣工作。

這是一個不使用 numpy、bisect 模塊或列表推導式的解決方案! 享受

list1=[1,2,4,8,16,32,64]
list2=[3,6,9,12,15,18,21]

correct={4:3, 8:3, 16:3}

lower=0
for t in list1:
  print t
  difference = 0
  index = lower
  while (difference == 0 and index<len(list2)-1):
    print "consider %d < %d and %d > %d" % (list2[index],t,list2[index+1],t)
    if list2[index]<t and list2[index+1] > t:
          lower = index
          upper = index + 1
          difference = list2[upper] - list2[lower]                              
          print "%d difference %d" % (t,list2[upper] - list2[lower])
          break
    index = index +1

  if t in correct.keys():
       assert(difference == correct[t])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM