简体   繁体   English

有关数字列表和间隔列表,请查找每个数字所在的时间间隔

[英]For a list of numbers and a list of intervals, find the interval in which each number is located

In a dictionary as follow: 在字典中如下:

example = {'Chr3' : [[1, 4], [5, 10], [13, 17]]}

How can I know in which list interval numbers 6 and 15 are located? 我如何知道区号6和15位于哪个列表中?

I can write a solution with a simple for loop: 我可以用一个简单的for循环编写一个解决方案:

for key in example.keys():
    for element in example[key]:
        if element[0]<= 6 <= element[1] or element[0] <= 15 <= element[1]:
            print element

But in my real problem, I don't have only two numbers but a list of numbers. 但在我真正的问题中,我不只有两个数字而是一个数字列表。

So, I was thinking if there is a more pythonic way of solving such a problem using special list or dictionary methods/functions? 那么,我在想是否有更多的pythonic方法使用特殊列表或字典方法/函数来解决这样的问题?

EDIT: Because of down votes, I clarify that I know how to write the code using another loop on top for a list of numbers, I was wondering if there is a better solution to the same problem, a more efficient one! 编辑:由于投票率下降,我澄清说我知道如何使用另一个循环编写代码以获取数字列表,我想知道是否有更好的解决方案来解决同样的问题,更有效率!

Create a dictionary which maps any number from your list of numbers to the corresponding interval(s) in example['Chr3'] . 创建一个字典,将您的数字列表中的任何数字映射到example['Chr3']的相应间隔。

>>> {x:filter(lambda y: y[0] <= x <= y[1], example['Chr3']) for x in lst}
{6: [5, 10], 15: [13, 17]}

Use a function! 使用功能!

example = {'Chr3' : [[1, 4], [5, 10], [13, 17]]}
example_nums = [3, 6, 11, 15, 17]

def find_interval(dct, num):
    for key, values in dct.items():
        for left, right in values:
            if left <= num <= right:
                return [left, right]

print(find_interval(example, 6))
print(find_interval(example, 15))

# Now you can use the function to find the interval for each number
for num in example_nums:
    print(num, find_interval(example, num))

Output 产量

[5, 10]
[13, 17]
3 [1, 4]
6 [5, 10]
11 None
15 [13, 17]
17 [13, 17]

https://repl.it/Bb78/4 https://repl.it/Bb78/4

Can you show more specific example of your list? 你能展示你的清单的更具体的例子吗? I think your code though would work by incorporating the line for a loop over the list: 我认为你的代码可以通过在列表中包含循环行来实现:

for x in my list:
    for key in example.keys():
         for element in example[key]:
            if element[0]<= x <= element[1] or element[0] <= x <= element[1]:
                   print element

Your code looks for any match not multiple matches, you can use a list comp with any 您的代码查找任何匹配而不是多个匹配,您可以使用any列表comp

from itertools import chain

pot = [6, 15]
print([[a, b] for a, b in  chain(*example.values()) if any(a <= i <= b for i in pot)])
[[5, 10], [13, 17]]

If you just care about any match you only really need to check the min and max of the potential numbers: 如果您只关心任何比赛,您只需要检查潜在数字的最小值最大值

pot = [6, 15,7,9,8]
from itertools import chain

mn,mx = min(pot), max(pot)
print([[a, b] for a, b in  chain(*example.values()) if mn <= a <= mx or mn <= b <= mx])

[[5, 10], [13, 17]]

So you are now doing constant work to check a and b instead of linear. 所以你现在正在不断地检查ab而不是线性。

Or using python3 you could use range and use in which is an O(1) operation in python3: 或者使用python3你可以使用范围和使用in其中是python3中的O(1)操作:

pot = [6, 15,7,9,8]
mn ,mx = min(pot),  max(pot)
inter = range(mn, mx+1)
print([[a, b] for a, b in chain(*example.values()) if a in inter or b in inter])
[[5, 10], [13, 17]]

暂无
暂无

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

相关问题 如何查找列表中的数字是否在列表中的两个数字的区间中? - How to find if a number in a list is in a interval of two numbers in a list? 找出不在区间列表中的最小数字 - Find the smallest number that is not in a list of intervals 列表 2 中的哪些数字比列表 1 中的每个数字更大和更小 - which numbers in list 2 are bigger and smaller than each number in list 1 从时间间隔列表中查找所有时间间隔集,其中一个集合中的每个EACH时间间隔与该集合中的所有时间间隔重叠 - From a List of Intervals, Finding all Sets of Intervals where EACH Interval in One Set Overlaps with All Intervals in that Set 在Python列表中查找彼此相距一定距离的数字 - Find numbers in Python list which are within a certain distance of each other 如何在列表中找到一对数字,使总和为给定数字 - How to find pair of numbers in a list which makes a sum as given number 通过专门将一个间隔添加到间隔列表中来插入间隔 - Interval insert by exclusively adding an interval to a list of intervals 在数字列表中找到最大的数字 - Find the greatest number in a list of numbers 如何从位于 3 索引的倍数的列表中获取所有数字 - How to get all numbers from list which are located at multiple of 3 index 我有一个列表,其中每个元素都是列表中的单个数字。 如何提取数字? - I have a list in which each element is a single number in a list. How do I extract the numbers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM