【问题标题】:Appending result in list or array python3在列表或数组python3中附加结果
【发布时间】:2018-08-15 17:42:49
【问题描述】:

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

我想做的是: 从第二个循环中收集最佳解决方案并将其附加到数组或列表中。 作为第一个循环的下一次运行将再次运行第二个循环,它将从第二个循环中收集最佳解决方案并将其附加到相同的数组或列表中。每次运行我将有两个解决方案,总共有六个解决方案。

问题是: 我想将“最佳解决方案”附加到下一次运行的相同索引位置。

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

run 1: 数组内的结果: index 0 "第一个最佳解决方案。" index 1 "次优方案。"

run 2: 数组内的结果: index 0 "第一个最佳解决方案。" “第一个最佳解决方案。”

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

【问题讨论】:

  • 您在此处生成的候选解决方案只是数字,因此在内存中存储 6 个值不是限制。您是否考虑过存储所有内容然后计算您的“最佳”?还是np.random.uniform 是繁重计算的替代品?
  • 您编写期望结果的方式相当难以理解。如果您只是给出数字示例,以及您希望在最终列表中列出哪些示例,则很可能很容易解决

标签: arrays python-3.x list numpy append


【解决方案1】:

更好地处理嵌套列表,例如 [[1st best answer,1st best answer],[..,..]...] 所以你应该在第二次运行中定义另一个列表。然后将其附加到结果列表中。这是修改后的代码:

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 中的所有值保存在索引 1 中,并将循环 2 中的所有值保存在索引 2 中,依此类推.....
  • @benaoumouad ,对不起,但不是我想要的情况!!!你能看看下一个cmets吗
  • 好的,现在我明白了。你可以再次检查我修改过的代码。但知道根据您要查找的内容,列表的长度为 2,每个子列表的长度为 3 @azeez
  • 谢谢@benaoumouad,但列表的长度取决于 generate_solutions 所以 (ii) 可以是 1 或大于 100,它们都是变量值。
【解决方案2】:

为了更清楚......

如果 (i) = 0 如果 (ii) = 0 此循环中的 min(solution) 将存储在索引 0 的 best_solutions 中 如果 (ii) = 1 此循环中的 min(solution) 将存储在索引 1 的 best_solutions 中 如果 (ii) = 2 此循环中的 min(solution) 将存储在索引 2 的 best_solutions 中 等等……

第二次运行: 如果 (i) = 1 如果 (ii) = 0 此循环中的 min(solution) 将存储在索引 0 的 best_solutions 中 如果 (ii) = 1 此循环中的 min(solution) 将存储在索引 1 的 best_solutions 中 如果 (ii) = 2 此循环中的 min(solution) 将存储在索引 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: 列表索引超出范围!!!???

【讨论】:

    【解决方案3】:

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

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

    最后一个循环将在哪里完成工作:) 感谢每一位提供想法和建议的人

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-07
      • 1970-01-01
      • 2017-03-28
      • 2018-08-15
      • 2021-01-11
      • 1970-01-01
      • 2020-03-09
      • 1970-01-01
      相关资源
      最近更新 更多