Python,检查数字是否在列表中的多个范围内. [英] Python, check if a number is in a range of many ranges in a list.

查看:1387
本文介绍了Python,检查数字是否在列表中的多个范围内.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有这样的整数列表:

If there are integer lists like these:

a_list = [2501, 2783, 3088, 3980, 465, 1001, 39392911, 39394382, 488955,489087, ......]
b_list = [474, 498, 47478821, 47479800, 3774, 8970, 484000, 486000......]

每2个数字表示一个自然数范围,例如, a_list 的范围为:

every 2 numbers indicate a range of natural numbers, for example, the ranges of a_list would be:

2501     2783      # 2501,2502,2503,2504,2505,2506,......,2783
3088     3980 
465      1001 
39392911 39394382 
488955   489087
......

对于给定的数字,请搜索其所属的范围,并且优先级为a_list> b_list,即,如果在a_list中找到了范围,请停止搜索并移至下一个要搜索的数字.

For a given number, search for the range where it belongs to, and with priority of a_list > b_list i.e. if a range is found in a_list, stop searching and move on to the next number for searching.

我进行了大约7分钟的搜索,搜索了50个数字.我有一个很大的数据集,可能需要用他的方式搜索2000万个数字.

I had test run for searching around 50 numbers which took about 7 minutes. I have a big dataset which could be 20 million numbers need to be searched in his way.

如何编写代码以使其更快?

=============更多的条件和信息=============

============= more conditions and information =============

  • 每个列表中的数字可能超过一万.
  • 最多可以搜索3000万个电话号码.
  • 列表的大小始终为n * 2
  • a_list:[1st<第二,第三< 4,......]
  • 列表中的数字可能会出现多次.
  • 优先级:a_list> b_list.

我的代码如下:

hasFound = 0

if hasFound == 0:
    for x, y in izip(*[iter(a_list)]*2):   # gives every 2 numbers
        if aNumber in range(x,y):
            a_list_counter +=1 
            hasFound = 1
            break

if hasFound == 0:       
    for x, y in izip(*[iter(b_list)]*2):
        if aNumber in range(x,y):
            b_list_counter += 1
            hasFound = 1
            break

非常感谢.

推荐答案

将它们全部扔进一本大词典中:

Toss them all in one big dictionary:

a_list = [2501, 2783, 3088, 3980, 465, 1001, 39392911, 39394382, 488955,489087, ......]
b_list = [474, 498, 47478821, 47479800, 3774, 8970, 484000, 486000......]
# into
ranges = {'a': [2501, 2783, 3088, 3980, 465, 1001, 39392911, 39394382, 488955,489087, ......],
          'b': [474, 498, 47478821, 47479800, 3774, 8970, 484000, 486000......]}

然后按顺序浏览每个列表,主要是您之前的操作方式:

Then go through each list in order, mostly the way you were doing it before:

numbers = [list of your target numbers]
scores = {} # dict to store results in

for number in numbers:

    for range_name in sorted(ranges):
        range_list = ranges[range_name]
        groups = zip(*[iter(range_list)] * 2)
        if any(start <= number < end for start,end in groups):
            scores.setdefault(range_name, 0) += 1

或者(我不确定这是否更快),您可以执行以下操作:

Alternatively (and I'm not sure if this is faster or not) you could do:

for number in numbers:
    for range_name in sorted(ranges):
        range = ranges[range_name]
        if sorted(range + [number]).index(number) % 2:
            scores.setdefault(range, 0) += 1

在这种情况下,您将一个新数字放入一个已排序的列表中,将其重新排序(使用TimSort可以快速排序),然后查看它是否落在两个现有数字之间.

In this case you're throwing a new number into a sorted list, re-sorting it (which is fast using TimSort), and seeing if it falls between two existing numbers.

这篇关于Python,检查数字是否在列表中的多个范围内.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆