列表2中的哪些数字大于和小于列表1中的每个数字

时间:2013-05-28 12:01:20

标签: python list search

我正在使用python。我有两个列表,列表1是7000个整数长,列表2是25000个整数。我想通过列表1中的每个数字,找到列表2中最接近的数字,这个数字更大,最接近的数字小于列表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中的数字更大的最小数字。有没有人有任何想法?感谢

4 个答案:

答案 0 :(得分:6)

您可以使用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中的任何元素。但如果需要,可以修复。

答案 1 :(得分:6)

这是使用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

答案 2 :(得分:3)

首先,您的示例不是有效代码,或者至少它不能执行您希望它执行的操作。如果你有

for i in list1:

然后我不是索引,而是list1的元素。首先,你要比较i和j,而不是list [i]和list [j]。

使用列表推导&gt;

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

您可能需要添加一个或两个以确保a和b存在,但它应该像这样工作。

答案 3 :(得分:0)

这是一个不使用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])