简体   繁体   English

查找列表b的子列表中的项目范围内的列表a中的项目

[英]Find if item in list a in range of items in sublist of list b

Let's say I have two lists. 假设我有两个清单。

x = [2,12,33,40,500]
y = ['1_4','9_11','38_50','300_400']

I would like to iterate through x and find determine if it is in the range of the other items in y (after splitting on '_'). 我想遍历x并确定它是否在y中的其他项目范围内(在'_'上分割后)。 If that is the case, it will break out of the loop since it does not need to check the others. 如果真是这样,它将退出循环,因为它不需要检查其他对象。 I'm not trying to see how many ranges it falls into, only that it occurs once. 我不是要查看它属于多少范围,而只是出现一次。

I think this code might work, but would like to double check. 我认为此代码可能有效,但想再次检查。

x = [2,12,33,40,500]
y = ['1_4','9_11','38_50','300_400']

dict = {}

for i in x:
    for j in y:
        j_1 = int(j.split('_')[0])
        j_2 = int(j.split('_')[1])
        if i in range(j_1,j_2):
            dict[i] = 'yes'
            break
        else:
            dict[i] = 'no'
            #the else statement is what's tricking me

The solution should yield the following in this example: 在此示例中,解决方案应产生以下结果:

dictt = {2:'yes',12:'no',33:'no',40:'yes',500:'no'}

Since you are testing a case versus any of the combinations within the list y , why not use any ? 由于您要对案例y和列表y任何组合进行测试,为什么不使用any

x = [2,12,33,40,500]
y = ['1_4','9_11','38_50','300_400']

y_new = [(int(a),int(b)) for i in y for a,b in zip(i.split("_"),i.split("_")[1:])]

l = {item:"Yes" if any(a<item<b for a,b in y_new) else "No" for item in x}

print (l)

#{2: 'Yes', 12: 'No', 33: 'No', 40: 'Yes', 500: 'No'}

How about checking if the number is in between any of the range of numbers in the y list. 如何检查数字是否在y列表中的任何数字范围之间。

>> x = [2,12,33,40,500]
>> y = ['1_4','9_11','38_50','300_400']
>> y_new = map(lambda x: tuple(map(int, x.split('_'))), y)
# y_new = [(1, 4), (9, 11), (38, 50), (300, 400)]
>> def check_in_range(number):
        found = 'no'
        for each_range in y_new:
             if each_range[0] <= number <= each_range[1]:
                 found = 'yes'
        return found
>> dict(zip(x, map(check_in_range, x)))
>> {2: 'yes', 12: 'no', 33: 'no', 40: 'yes', 500: 'no'}

Note: Otherwise, if you are using Python 2, always use xrange and not range . 注意:否则,如果您使用的是Python 2,请始终使用xrange而不是range xrange will not keep all the numbers in the memory which range does. xrange不会将所有数字保留在该范围内的内存中。 This will be an issue when the range is bigger. 当范围更大时,这将是一个问题。 Python3 will default to xrange. Python3将默认为xrange。

It does work. 确实有效。 As you wrote it. 当你写的时候。

x = [2,12,33,40,500]
y = ['1_4','9_11','38_50','300_400']
dict = {}

for i in x:
    for j in y:
        j_1 = int(j.split('_')[0])
        j_2 = int(j.split('_')[1])
        if i in range(j_1,j_2):
            dict[i] = 'yes'
            break
        else:
            dict[i] = 'no'

Returns output: 返回输出:

{2: 'yes', 12: 'no', 33: 'no', 40: 'yes', 500: 'no'}

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

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