请假设给出以下字典
sources_targets =
{'front': {'source': [[0.021025050381526675, -0.39686011326197257, 3.328947963092819], [1.0601052368302668, -0.3938359761868055, 3.3247223740425893], [1.0543731204008824, -0.038184154352961984, 2.941639590795943], [0.017868184643970383, -0.0445863307249157, 2.9604912584916665]], 'target': [[-250.0, 0.0, 60.0], [-250.0, 0.0, -60.0], [-190.0, 0.0, -60.0], [-190.0, 0.0, 60.0]]}, 'left': {'source': [[-0.9522471062122733, -1.8444069007997075, 5.372044839796925], [-0.9665739520089994, -1.001259794819009, 5.0057689609608005], [0.9940538769534978, -1.851333804840362, 5.340677879647542], [0.9959517362506759, -1.0049420919111534, 4.942663843894899]], 'target': [[60.0, 0.0, 140.0], [60.0, 0.0, 80.0], [-60.0, 0.0, 140.0], [-60.0, 0.0, 80.0]]}, 'right': {'source': [[-0.8596841529333474, -3.0721166255322663, 4.182871479604773], [-0.8796404109762729, -2.117062488877432, 4.147040556143069], [1.0791152756424247, -2.0436646487085532, 4.08578012939533], [1.0951903113036177, -2.994375693306352, 4.124102127893507]], 'target': [[-60.0, 0.0, -140.0], [-60.0, 0.0, -80.0], [60.0, 0.0, -80.0], [60.0, 0.0, -140.0]]}, 'rear': {'source': [[0.08792816743383122, -0.5260295091566244, 3.0182522276468458], [0.9012540916522604, -0.5012267882763559, 3.0172622143554695], [0.8942115223224005, -0.15635208604951806, 2.6353057539009934], [0.08814313470840558, -0.18017837896764446, 2.6579174137231463]], 'target': [[250.0, 0.0, -40.0], [250.0, 0.0, 60.0], [190.0, 0.0, 60.0], [190.0, 0.0, -40.0]]}}
bundle_names = ['front', 'left', 'right', 'rear']
我想获得所有来源的列表和所有目标的另一个列表。
此外,它需要在单行列表理解中完成。
我的成功尝试是
sources3d = [st[1] for name in self._bundle_names for st in sources_targets[name].items() if st[0] == "source"]
targets3d = [st[1] for name in self._bundle_names for st in sources_targets[name].items() if st[0] == "target"]
具有正确的输出(例如,对于“来源”)
[[[0.021025050381526675, -0.39686011326197257, 3.328947963092819], [1.0601052368302668, -0.3938359761868055, 3.3247223740425893], [1.0543731204008824, -0.038184154352961984, 2.941639590795943], [0.017868184643970383, -0.0445863307249157, 2.9604912584916665]], [[-0.9522471062122733, -1.8444069007997075, 5.372044839796925], [-0.9665739520089994, -1.001259794819009, 5.0057689609608005], [0.9940538769534978, -1.851333804840362, 5.340677879647542], [0.9959517362506759, -1.0049420919111534, 4.942663843894899]], [[-0.8596841529333474, -3.0721166255322663, 4.182871479604773], [-0.8796404109762729, -2.117062488877432, 4.147040556143069], [1.0791152756424247, -2.0436646487085532, 4.08578012939533], [1.0951903113036177, -2.994375693306352, 4.124102127893507]], [[0.08792816743383122, -0.5260295091566244, 3.0182522276468458], [0.9012540916522604, -0.5012267882763559, 3.0172622143554695], [0.8942115223224005, -0.15635208604951806, 2.6353057539009934], [0.08814313470840558, -0.18017837896764446, 2.6579174137231463]]]
我使用和通过索引访问元组访问内部字典的方式.items()
似乎很麻烦。
我想以某种方式访问
[st["source"] for name in self._bundle_names for st in sources_targets[name]]
这不起作用,但会更清洁。
我相信有一种方法可以正确地做到这一点。