字典out_trajectories2 = {}包含如下多条信息 {Drone: 9: [Tour w depot: (270.1543358807301, 439.7385517463642), nnodes: 2, Tour w depot: (270.1543358807301, 439.7385517463642)} {Drone: 9: [Tour w depot: (370.1543358807301, 539.7385517463642), nnodes: 2, Tour w depot: (370.1543358807301, 539.7385517463642)} 如何将字典中每条信息都送入如下函数 alg = TotalGreedyCoverage(aoi2, out_trajectories2, max_rounds, debug=False)
时间: 2024-02-21 17:56:40 浏览: 65
你可以使用一个 for 循环遍历字典中的每条信息,并将其作为参数传递给函数 TotalGreedyCoverage。示例代码如下:
```python
for key, value in out_trajectories2.items():
alg = TotalGreedyCoverage(aoi2, {key: value}, max_rounds, debug=False)
```
这里使用 items 方法遍历字典,将每个键值对作为元组返回。然后将每个键值对作为参数传递给函数 TotalGreedyCoverage,构造一个新的字典作为函数的第二个参数。注意,这里将每个键值对用花括号括起来构成一个字典,以保证每个键值对作为一个整体被传递给函数。
相关问题
字典out_trajectories2 = {}包含如下多条信息 {Drone: 9: [Tour w depot: (270.1543358807301, 439.7385517463642), nnodes: 2, Tour w depot: (270.1543358807301, 439.7385517463642)} {Drone: 9: [Tour w depot: (370.1543358807301, 539.7385517463642), nnodes: 2, Tour w depot: (370.1543358807301, 539.7385517463642)} ,如何取第一条信息
你可以使用以下代码取出第一条信息:
```
info = list(out_trajectories2.values())[0]
```
这将返回字典中第一个键值对的值,即第一条信息。你可以将其存储在一个变量中,以便后续使用。
for drone in drones1: trajs1 = trajectories_builder.compute_trajectories(drone, depots[0]) out_trajectories1[drone] = trajs1, for drone in drones2: trajs2 = trajectories_builder.compute_trajectories(drone, depots[0]) out_trajectories2[drone] = trajs2, 如何修改程序能使得out_trajectories1[drone]和out_trajectories2[drone]的内容加到一起,通过out_trajectories[drone]输出
你在循环中判断 drone 属于哪个列表,并将轨迹信息累加到 out_trajectories[drone] 中,如下所示:
```
out_trajectories = {}
for drone in drones1 + drones2:
trajs = trajectories_builder.compute_trajectories(drone, depots[0])
if drone in drones1:
if drone in out_trajectories1:
out_trajectories[drone] = out_trajectories1[drone] + trajs
else:
out_trajectories[drone] = trajs
else:
if drone in out_trajectories2:
out_trajectories[drone] = out_trajectories2[drone] + trajs
else:
out_trajectories[drone] = trajs
print(out_trajectories[drone])
```
这样修改后,程序会将 out_trajectories1[drone] 和 out_trajectories2[drone] 的内容加到一起,并将结果存储到 out_trajectories[drone] 中。需要注意的是,如果某个 drone 在两个列表中都出现,需要将两个列表中对应的轨迹信息累加起来。
阅读全文