Python 搜索字典值(坐标列表)并将其与列表(bbox)和 return 特定键进行比较
Python search and compare Dict-values (coordinates-list) to a list (bbox) and return specific key
我有一个字典,其值如下(坐标 = [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 的每个 value/values 上迭代 bb_list 的值,并且应该通过类似的方式比较它们:
if bx1 >= cx1 and <= cx2 and if by1 >= cy1 and <= cy2 or if bx2 >= cx1 and <= cx2 and if by2 <= cy2 and >= cy1
欢迎任何帮助,谢谢!
这个怎么样。
代码
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
我有一个字典,其值如下(坐标 = [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 的每个 value/values 上迭代 bb_list 的值,并且应该通过类似的方式比较它们:
if bx1 >= cx1 and <= cx2 and if by1 >= cy1 and <= cy2 or if bx2 >= cx1 and <= cx2 and if by2 <= cy2 and >= cy1
欢迎任何帮助,谢谢!
这个怎么样。
代码
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