[英]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))
[5, 10]
[13, 17]
3 [1, 4]
6 [5, 10]
11 None
15 [13, 17]
17 [13, 17]
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. 所以你现在正在不断地检查
a
和b
而不是线性。
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.