我有一個這樣的值的字典(坐標 = [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 and if by1 >= cy1 and <= cy2 or if bx2 >= cx1 and <= cx2 and if by2 <= cy2 and >= cy1
歡迎任何幫助,謝謝!
uj5u.com熱心網友回復:
這個怎么樣。
代碼
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/334497.html
上一篇:根據串列過濾字典
