在列表或数组python3中追加结果 [英] Appending result in list or array python3

查看:125
本文介绍了在列表或数组python3中追加结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python-3.x,并且我有两个循环,第一个是运行次数(3次),第二个是生成解决方案(2次)

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.

在我的情况下,数组最终的大小为6,但我希望它的大小为3,其中每个索引将包含两个值(最佳解决方案)

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)

运行1:结果在数组中: index 0第一个最佳解决方案." 索引1第二好的解决方案."

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

运行2:结果在数组内: index 0第一个最佳解决方案." 第一个最佳解决方案."

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

index 1第二好的解决方案." 第二好的解决方案."

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)

推荐答案

最后,我找到了解决问题的最佳方法,这是我编写的最终代码:

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 :) thanks for every one for giving ideas and advises

这篇关于在列表或数组python3中追加结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆