for i, j, k, l in product(range(10, 21), range(5, 11), range(2, 11), range(1, 3)): data["normalSpeed"] = i / 3.6 data["minTurningRadius"] = j data["breadth"] = k data["jobType"] = l # 小地块 data["map"]["outerbd"] = outerbd_small data["map"]["entrance"] = entrance_small data["map"]["exit"] = exit_small json_str = json.dumps(data) res = requests.post(url, json_str).text filename = os.path.join(desktop_path_file, f'normalSpeed={i}, minTurningRadius={j}, breadth={k}, jobType={l}, ' f'小地块.txt') # filename = f'normalSpeed={i}, minTurningRadius={j}, breadth={k}, 小地块.txt' with open(filename, 'w') as file: file.write(res) print(f'写入成功,速度为{i},转弯半径为{j},幅宽为{k},作业模式为{l},小地块') logger.info(f'写入成功,速度为{i},转弯半径为{j},幅宽为{k},作业模式为{l},小地块') time.sleep(10) plot_route_from_file(filename, f'normalSpeed={i}, minTurningRadius={j}, breadth={k}, jobType={l}, 小地块') # 大地块 data["map"]["outerbd"] = outerbd_large data["map"]["entrance"] = entrance_large data["map"]["exit"] = exit_large json_str = json.dumps(data) res = requests.post(url, json_str).text filename = os.path.join(desktop_path_file, f'normalSpeed={i}, minTurningRadius={j}, breadth={k}, jobType={l}, ' f'大地块.txt') with open(filename, 'w') as file: file.write(res) print(f'写入成功,速度为{i},转弯半径为{j},幅宽为{k},作业模式为{l},大地块') logger.info(f'写入成功,速度为{i},转弯半径为{j},幅宽为{k},作业模式为{l},大地块') time.sleep(10) plot_route_from_file(filename, f'normalSpeed={i}, minTurningRadius={j}, breadth={k}, jobType={l}, 大地块')。大地块和小地块只有map区别。能不能合并代码?要求大地块小地块都能遍历到
时间: 2024-04-15 11:27:03 浏览: 87
当然可以合并代码来遍历大地块和小地块。你可以尝试以下修改:
```python
for i, j, k, l in product(range(10, 21), range(5, 11), range(2, 11), range(1, 3)):
data["normalSpeed"] = i / 3.6
data["minTurningRadius"] = j
data["breadth"] = k
data["jobType"] = l
for block_type in ["小地块", "大地块"]:
if block_type == "小地块":
data["map"]["outerbd"] = outerbd_small
data["map"]["entrance"] = entrance_small
data["map"]["exit"] = exit_small
else:
data["map"]["outerbd"] = outerbd_large
data["map"]["entrance"] = entrance_large
data["map"]["exit"] = exit_large
json_str = json.dumps(data)
res = requests.post(url, json_str).text
filename = os.path.join(desktop_path_file, f'normalSpeed={i}, minTurningRadius={j}, breadth={k}, jobType={l}, {block_type}.txt')
with open(filename, 'w') as file:
file.write(res)
print(f'写入成功,速度为{i},转弯半径为{j},幅宽为{k},作业模式为{l},{block_type}')
logger.info(f'写入成功,速度为{i},转弯半径为{j},幅宽为{k},作业模式为{l},{block_type}')
time.sleep(10)
plot_route_from_file(filename, f'normalSpeed={i}, minTurningRadius={j}, breadth={k}, jobType={l}, {block_type}')
```
这样修改后,代码将在每组参数循环内部遍历大地块和小地块,分别进行相应的操作。
阅读全文