简体   繁体   English

如何在python中检查列表值范围内的值

[英]How to check a value in a list value range in python

I have a value to check in the list range 我有一个值可以检查列表范围

Example: 例:

    recipies = int(input("enter:"))
print(recipies)
interval =[]
for i in range(recipies):
    x = (input().split())
    interval.append(x)
print(interval)
agroup =int(input("enter:"))
print(agroup)
qali = []
for i in range(agroup):
    x = (input().split())
    qali.append(x)
print(qali)
for cmp in qali:
    toa = cmp[1:]

Input:
4
1 4
3 10
2 6
5 8
3
1 5
2 2 6
3 1 10 9

Output:
3
4
2

Here i want to check weather the value of toa available in the interval if available i want to print how many times that value available in the given intervals,same way i want to check values in quali(list) 在这里,我想检查天气间隔中可用的toa值(如果可用),我想打印给定间隔中可用的toa值多少次,以同样的方式我想检查quali(列表)中的值

You can use following list comprehensions : 您可以使用以下列表推导:

>>> f_list=[map(int,i.split(',')) for i in li]
>>> ['yes' if toa in range(i,j) else 'No' for i,j in f_list]
['yes', 'yes']

Fist you need to extract your ranges that it can be done with splitting your strings with , and convert to int with map function. 首先,您需要提取范围,可以使用分割字符串来完成范围,并使用map函数转换为int

Then you can use a list comprehension like following to check the member-ship : 然后,您可以像下面这样使用列表理解来检查会员资格:

['yes' if toa in range(i,j) else 'No' for i,j in f_list]

But not that range will contains the start but not end if you dont want such things you need to increase your start when you want to create the range : 但是,如果您不希望出现这种情况,那么该range将不包含start而不会包含end当您要创建范围时,需要增加起点:

>>> ['yes' if toa in range(i+1,j) else 'No' for i,j in f_list]
['yes', 'No']

You'll have to parse each string into a start and stop value (integers): 您必须将每个字符串解析为一个起始值和终止值(整数):

ranges = ['1,9', '2,10']

for start_stop in ranges:
    start, stop = map(int, start_stop.split(','))
    if toa in range(start, stop):
        print('yes')

Instead of creating a new range() object you can just use comparison operators: 除了创建新的range()对象,您还可以使用比较运算符:

for start_stop in yourlist:
    start, stop = map(int, start_stop.split(','))
    if start <= toa < stop:
        print('yes')

This'll be faster as you don't need to create an additional range() object. 这将更快,因为您无需创建其他range()对象。

You probably want to store those integers instead: 您可能想存储这些整数:

ranges = [[int(i) for i in start_stop.split(',')] for start_stop in ranges]

to not have to parse them each and every time. 不必每次都解析它们。

If you need to do a lot of these tests, against a large number of intervals, consider using an Interval Tree data structure instead. 如果您需要针对大量间隔进行大量此类测试,请考虑改为使用间隔树数据结构。 In an Interval Tree finding matching ranges (intervals) takes O(log N) time, vs. O(N) for the straight search. 在间隔树中,找到匹配范围(间隔)需要O(log N)时间,而直接搜索需要O(N)。 So for 10000 ranges to test against, it'll only take 100 steps to find all matches, vs 10000 steps using the straight search used above. 因此,要测试10000个范围,只需100个步骤即可找到所有匹配项,而使用上面的直接搜索则只需10000个步骤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM