Appending Result In List Or Array Python3
Solution 1:
better work on nested list like [[1st best answer,1st best answer],[..,..]...] so you should define an other list in the second run . then append it to the result list . here's the modified code:
for i in range (run):
lower = 300upper = 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.4323434upper = 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)
Solution 2:
to make it more clear....
if the (i) = 0 if the (ii) = 0 the min(solution) from this loop will be stored in best_solutions in the index 0 if the (ii) = 1 the min(solution) from this loop will be stored in best_solutions in the index 1 if the (ii) = 2 the min(solution) from this loop will be stored in best_solutions in the index 2 and so on......
second run: if the (i) = 1 if the (ii) = 0 the min(solution) from this loop will be stored in best_solutions in the index 0 if the (ii) = 1 the min(solution) from this loop will be stored in best_solutions in the index 1 if the (ii) = 2 the min(solution) from this loop will be stored in best_solutions in the index 2 and so on......
I hope this will clarify what I want to do!!
I tried to store them by index but it gives me an error:
for i in range (run):
lower = 300upper = 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.4323434upper = 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: list index out of range !!!????
Solution 3:
Finally, I find the best way to solve my problem, this the final code that I wrote:
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 inrange (run):
lower = 300
upper = 500
number_of_solutions = 50
generate_solutions = 5####sub list
solution = []
for ii inrange (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 inrange(generate_solutions):
for ii inrange (run):
best_sol_sorted.append(best_solutions[ii][i])
final_result.append(best_sol_sorted)
best_sol_sorted = []
where the last loop will do the job :) thanks for every one for giving ideas and advises
Post a Comment for "Appending Result In List Or Array Python3"