我正在使用python-3.x并且我有第一个循环,一个是运行次数(3次),第二次是生成解决方案(2次)
我想做的是: 从第二个循环中收集最佳解决方案并将其附加到数组或列表中。 第一个循环的下一个运行将再次运行第二个循环,它将从第二个循环收集最佳解决方案并将其附加到相同的数组或列表。我将在每次运行中有两个解决方案,总计将有六个解决方案。
问题是: 我想将“最佳解决方案”附加到下一次运行的相同索引位置。
在我的情况下,数组最终的大小为6,但我希望它的大小为3,其中每个索引将包含两个值(最佳解决方案)
运行1:数组内的结果: index 0“第一个最佳解决方案。” index 1“第二个最佳解决方案。”
运行2:数组内部的结果: index 0“第一个最佳解决方案。” “第一个最好的解决方案。”
索引1“第二个最佳解决方案。” “第二个最好的解决方案。”
如果您查看代码和结果,我会更清楚我想要做什么? 您可以提供的任何建议或帮助将非常感激
import matplotlib.pyplot as plt
import math
import random
import numpy as np
import pylab
from numpy import median
import os
import subprocess as sp
run = 3
best_solutions = [np.empty(0)]
del best_solutions[0]
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
for i in range (run):
lower = 300
upper = 500
number_of_solutions = 50
generate_solutions = 2
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
for ii in range (generate_solutions):
list_of_solutions = np.random.uniform(lower, upper, number_of_solutions)
best_solutions.append(min(list_of_solutions))
lower = lower - 30.4323434
upper = upper - 90.634555
del (number_of_solutions)
del (lower)
答案 0 :(得分:1)
更好的嵌套列表工作,如[[第一个最佳答案,第一个最佳答案],[..,..] ......] 所以你应该在第二次运行中定义另一个列表。然后将其附加到结果列表。这是修改后的代码:
for i in range (run):
lower = 300
upper = 500
number_of_solutions = 50
generate_solutions = 2
####sub list
first_solution = []
second_solution = []
for ii in range (generate_solutions):
list_of_solutions = np.random.uniform(lower, upper,
number_of_solutions)
#### append to the sub_list
if ii == 1 :
first_solution.append(min(list_of_solutions))
if ii == 2 :
second_solution.append(min(list_of_solutions))
lower = lower - 30.4323434
upper = upper - 90.634555
del (number_of_solutions)
del (lower)
#### append the sub_list to best_solutions after closing the loop
best_solution.append(fisrt_solution)
best_solution.append(second_solution)
答案 1 :(得分:0)
使其更清晰......
如果(i)= 0 如果(ii)= 0 来自此循环的min(解决方案)将存储在索引0中的best_solutions中 如果(ii)= 1 来自此循环的min(解决方案)将存储在索引1中的best_solutions中 如果(ii)= 2 来自此循环的min(解决方案)将存储在索引2中的best_solutions中 等等......
第二轮: 如果(i)= 1 如果(ii)= 0 来自此循环的min(解决方案)将存储在索引0中的best_solutions中 如果(ii)= 1 来自此循环的min(解决方案)将存储在索引1中的best_solutions中 如果(ii)= 2 来自此循环的min(解决方案)将存储在索引2中的best_solutions中 等等......我希望这能澄清我想做的事情!
我试图通过索引存储它们,但它给了我一个错误:
for i in range (run):
lower = 300
upper = 500
number_of_solutions = 50
generate_solutions = 2
####sub list
solution = []
for ii in range (generate_solutions):
list_of_solutions = np.random.uniform(lower, upper, number_of_solutions)
#### append to the sub_list
solution.append(min(list_of_solutions))
lower = lower - 30.4323434
upper = upper - 90.634555
#### append the sub_list to best_solutions
best_solutions[ii].append(solution[ii]) ############ here I tried to store them by index
del (number_of_solutions)
del (lower)
IndexError:列表索引超出范围!!! ????
答案 2 :(得分:0)
最后,我找到解决问题的最佳方法,这是我写的最终代码:
import random
import numpy as np
run = 3
best_solutions = [np.empty(0)]
best_sol_sorted = []
del best_solutions[0]
final_result = [np.empty(0)]
del final_result[0]
for i in range (run):
lower = 300
upper = 500
number_of_solutions = 50
generate_solutions = 5
####sub list
solution = []
for ii in range (generate_solutions):
list_of_solutions = np.random.uniform(lower, upper, number_of_solutions)
#### append to the sub_list
solution.append(min(list_of_solutions))
lower = lower - 30.4323434
upper = upper - 90.634555
best_solutions.append(solution)
for i in range(generate_solutions):
for ii in range (run):
best_sol_sorted.append(best_solutions[ii][i])
final_result.append(best_sol_sorted)
best_sol_sorted = []
最后一个循环将完成工作:) 感谢每个人提供想法和建议