【问题标题】:Python search and compare Dict-values (coordinates-list) to a list (bbox) and return specific keyPython搜索并将字典值(坐标列表)与列表(bbox)进行比较并返回特定键
【发布时间】:2021-10-22 01:58:14
【问题描述】:

我有一个具有这样值的字典(坐标 = [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 = cy1 and = cx1 and = cy1

欢迎任何帮助,谢谢!

【问题讨论】:

    标签: python list loops dictionary compare


    【解决方案1】:

    这个怎么样。

    代码

    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
    

    【讨论】:

    • 我是个菜鸟。工作得很好,非常感谢!
    猜你喜欢
    • 2022-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多