Python搜索并比较字典值(coordinates-list)和列表(bbox)并返回特定的键 &



我有一个值像这样的字典(坐标= [cx1, cx2, cy1, cy2]):

> co_list =
[
{
"data.01": [
6.9490666,
47.4897206,
7.0073678,
47.5169333
]
},
{
"data.02": [
6.9493157,
47.4627392,
7.0075872,
47.4899521
]
}
]

从用户输入中我得到了一个包含4个坐标的列表(bbox = [bx1, by1, bx2, by2])

bb_list = [6.974532, 47.469739, 7.000004, 47.481432]

我想检查bb_list是否符合co_list的值,如果左下角或右上角在一定范围内,则返回相应的键。我如何在co_list的每个值/值上迭代bb_list的值,它们应该像这样进行比较:

如果bx1>= cx1 and <= cx2且by1>= cy1 and <= cy2或bx2>= cx1 and <= cx2且by2 <= cy2

欢迎任何帮助,谢谢!

这个怎么样

co_list = [
{
"data.01": [
6.9490666,
47.4897206,
7.0073678,
47.5169333
]
},
{
"data.02": [
6.9493157,
47.4627392,
7.0075872,
47.4899521
]
}
]
bb_list = [6.974532, 47.469739, 7.000004, 47.481432]
# Loop thru the list
for c in co_list:
# Loop thru dict
for k, v in c.items():
cx1 = v[0]
cx2 = v[1]
cy1 = v[2]
cy2 = v[3]
# take the element of a list
bx1 = bb_list[0]
by1 = bb_list[1]
bx2 = bb_list[2]
by2 = bb_list[3]
if (bx1 >= cx1 and bx1 <= cx2 and by1 >= cy1 and by1 <= cy2 
or bx2 >= cx1 and bx2 <= cx2 and by2 <= cy2 and by2 >= cy1):
print(k)

data.01
data.02

最新更新