12275

Appending result in list or array python3

Question:

I am using python-3.x and I have tow loops first one which is number of runs (3 times) the second one to generate solutions (2 times)

what I want to do is: Collect the best solution from the second loop and append it to an array or list. The next run which is the first loop will run the second loop again, and it will collect the best solution from the second loop and append it to the same array or list. Which I will have two solutions in each run the total will be six solutions.

the problem is: I want to append the "best solution" to same index location for the next run.

in my case, the array will end up with a size of 6, but I want it to be a size of 3 where each index will include two values (best solution)

run 1: result inside the array: index 0 "The first best solution." index 1 "The second best solution."

run 2: result inside the array: index 0 "The first best solution." "The first best solution."

index 1 "The second best solution." "The second best solution."

If you take a look at the code and the result it will be more clear what I am trying to do? any advice or help you could provide would be much appreciated

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)

Answer1:

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 = 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)

Answer2:

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 = 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: list index out of range !!!????

Answer3:

<strong>Finally, I find the best way to solve my problem, this the final code that I wrote:</strong>

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 = []

where the last loop will do the job :) <strong>thanks for every one for giving ideas and advises</strong>

Recommend

  • Defining colors of Matplotlib 3D bar plot
  • algebraic expressions in python
  • matplotlib animate memory leak
  • matplotlib plots too many ticks
  • 'ImportError: cannot import name cbook' when using PyCharm's Profiler
  • Create colour picture from greyscale picture
  • Proper Mutational scale value in matplotlib
  • ModuleNotFoundError: No module named 'matplotlib'
  • matplotlib's colormap
  • How to show matplotlib plot from a figure object
  • matplotlib issues when nan first in list
  • Issue clearing the Tkinter Canvas widget using also matplotlib
  • Dendrogram or Other Plot from Distance Matrix
  • How to add a filter to Active Admin dashboard?
  • Is it possible to run an application built on sql server 2008 to run with 2005
  • Run EF6 Query in separate Thread on WinForm Button Click Event
  • AVCaptureSession VS UIImagePickerController camera preview
  • Accessing the variables from a PHP Anonymous Function
  • Salesforce Different WSDL files and when to use
  • msbuild create itemgroup from property group
  • Silverlight DependencyProperty.SetCurrentValue Equivalent
  • Eloquent paginate function in Slim 3 project using twig
  • Zurb Foundation _global.scss meta styles for js?
  • How to determine if there are bytes available to be read from boost:asio:serial_port
  • How do I exclude a dependency in provided scope when running in Maven test scope?
  • $wpdb not working in file of WordPress plugin
  • Meteor helpers not available in Angular template
  • formatting the colorbar ticklabels with SymLogNorm normalization in matplotlib
  • Fill an image in a square container while keeping aspect ratio
  • Join two tables and save into third-sql
  • Convert array of 8 bytes to signed long in C++
  • How to make Safari send if-modified-since header?
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Matplotlib draw Spline from multiple points
  • Buffer size for converting unsigned long to string
  • Hits per day in Google Big Query
  • Understanding cpu registers
  • How to get Windows thread pool to call class member function?
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices