Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用多个列表中的值创建数组_Python_Arrays_Python 3.x - Fatal编程技术网

Python 使用多个列表中的值创建数组

Python 使用多个列表中的值创建数组,python,arrays,python-3.x,Python,Arrays,Python 3.x,我创建了一些代码,基本上创建了100个不断增大的列表,因此第一个列表只有一个元素,第二个列表有两个元素,等等。我创建了一些代码,返回每个列表的最小值以及该值的索引。我需要做的是能够将所有这些值编译成100个元素的数组。有人能看看我的代码,帮我解决这个问题吗 我尝试过使用循环,但无法产生任何结果 这是我正在使用的代码 def optimisation(c,n): import numpy as np import itertools optimal=n*[0] #create empty s

我创建了一些代码,基本上创建了100个不断增大的列表,因此第一个列表只有一个元素,第二个列表有两个元素,等等。我创建了一些代码,返回每个列表的最小值以及该值的索引。我需要做的是能够将所有这些值编译成100个元素的数组。有人能看看我的代码,帮我解决这个问题吗

我尝试过使用循环,但无法产生任何结果

这是我正在使用的代码

def optimisation(c,n):
 import numpy as np
 import itertools
 optimal=n*[0] #create empty set for optimal values for each segment
 optimal[0]=0 #set error value for first partition equal to zero
 for j in range(2,n): #finds the minimum error for each partition
     samples=[]
     for i in range(1,j): #finds the minimal error for each line segment
         samples.append(find_error(i,j))
     samples=np.trim_zeros(samples)
     #print(samples)
     val=n*[0]
     idx=n*[0]
     (val, idx) = min((val, idx) for (idx, val) in enumerate(samples))
     print(val,idx)
 for j in range(1,n):
    for i in range(1,j):
        optimal[j]= val+c+min(optimal[i-1])
只要给出命令,该代码就会产生如下输出

optimisation(5,100)
这将为左侧显示的每个列表及其右侧对应的索引生成一个最小值列表。我将不显示完整的列表及其所有元素,因为其大小将是巨大的

0.05234944 0
0.23141890982 0
0.0606494102718 2
0.125266172852 0
0.468722738436 4
0.161172451696 0
0.0368827349571 6
0.406437961967 6
0.058411270794 0
0.17900671743 2
0.295569771275 5
0.811505725221 10
0.664073724598 1
1.09909164122 12
0.855491327741 0
1.10540230556 1
0.422713454883 1
1.10272876541 13
1.29587621875 8
1.51194852411 1
2.26497408277 14
3.0111216055 1
1.50627139464 10
3.16516961248 3
2.20094742717 12
3.14639332131 3
2.29403930871 17
2.56656811255 0
5.58629931099 0
5.17459137087 14
3.94986666265 26
6.17020493729 1
6.24984116656 3
6.70767705495 5
6.5236306679 1
7.0887021377 0
7.82672887258 26
5.8351667607 19
5.95734934174 9
11.7341734401 0
14.8294023759 20
8.36809353684 38
12.4300969144 5
15.0063530073 19
13.5851074804 4
20.2610057741 20
17.8737428506 8
17.9420111426 41
17.7309372978 32
24.1566446052 8
26.1398912466 25
23.5973369461 1
26.6403811411 38
34.0977995904 8
32.7782334418 51
24.3663099693 15
39.1511730112 23
43.0494833023 49
39.8814013457 25
47.3142093575 47
47.7307536374 30
49.7080368308 18
52.13139424 18
61.0036863302 5
67.7867571815 60
74.4165905127 54
62.7673033574 30
78.4737938102 5
82.0223279841 29
84.0406948927 19
98.4941295494 28
92.6064227071 65
120.043135174 2
119.294931784 21
129.513913827 14
129.86828615 5
155.771315864 35
157.91467315 17
159.894102894 35 
174.519517561 1
181.339901926 70
199.118265967 0
175.772059217 31
220.748358524 82
221.246388848 5
220.620796722 64
256.067301454 30
256.591536797 19
277.194513836 39
294.652427163 3
335.961156216 12
315.188196294 34
329.288057775 29
375.147283979 39
378.195190729 25
407.371302973 22
393.743613132 47
416.971899107 93 

我正试图将所有这些值编译成一个数组,这样它的形式为[0.05234944,0.23141890982,…,416.971899107],索引的形式为[0,0,…,93]

既然你的帖子中没有包含它,我制作了一个模型
find\u error
函数

find_error = lambda i, j: random.random()
它除了创建随机值之外什么都不做。当然,您需要使用自己的功能

还有,我不知道你想用它做什么

for j in range(1, n):
    for i in range(1, j):
        optimal[j] = val + c + min(optimal[i - 1])
这将导致错误,因为
min
需要一个序列,但
optimal[i-1]
始终是一个整数

我添加了两个列表
val\u list
idx\u list

val_list = []
idx_list = []
它将存储您的值并最终打印它们

import itertools
import numpy as np
import random

find_error = lambda i, j: random.random()

def optimisation(c, n):
    val_list = []
    idx_list = []
    optimal = n * [0]         # create empty set for optimal values for each segment
    optimal[0] = 0            # set error value for first partition equal to zero
    for j in range(2, n):     # finds the minimum error for each partition
        samples = []
        for i in range(1, j): # finds the minimal error for each line segment
            samples.append(find_error(i, j))
        samples = np.trim_zeros(samples)
        #print(samples)
        val = n * [0]
        idx = n * [0]
        (val, idx) = min((val, idx) for (idx, val) in enumerate(samples))
        #print(val, idx)
        val_list.append(val)
        idx_list.append(idx)
    print(val_list)
    print(idx_list)
    #for j in range(1, n):
    #    for i in range(1, j):
    #        optimal[j] = val + c + min(optimal[i - 1])

optimisation(5, 100)
其中打印:

[0.6748920956425049, 0.20157630160777396, 0.541618597753885, 0.2768964675507879, 0.2976946308913999, 0.3766115048894233, 0.13759194561337484, 0.14987103061621476, 0.014534443068388692, 0.0031480380328120505, 0.034201372897887716, 0.062040806572080553, 0.12730306740011677, 0.24416492868734152, 0.16290838010569786, 0.0018081174520545584, 0.08755932761277996, 0.019842603354791377, 0.020590886194172042, 0.0036695714888764774, 0.12112028957966736, 0.011358619347358823, 0.0066146447764062755, 0.001394609349268272, 0.029586896510528815, 0.002756814742788438, 0.0583562604851291, 0.04284009994467686, 0.016023085954640637, 0.022566876451069584, 0.033772524651475844, 0.005337819133243049, 0.008672534870379667, 0.07821234490854523, 0.039383235142540496, 0.06648877424948574, 0.02217050629348072, 0.0011878402013764111, 0.0076300194701606205, 0.05055673273571437, 0.005344908265560777, 0.007028301085901245, 0.04165421018218529, 0.049295508008472266, 0.05093904397035376, 0.0034540854779047114, 0.02459850682010234, 0.0025335204612035866, 0.05614152881910395, 0.006084903262635, 0.0009059035342229294, 0.01200570503260534, 0.02220929842526087, 0.015366700312140713, 0.00958513758114643, 0.07156333927540381, 0.04216894275052541, 0.008754644639532905, 0.0002495576990957371, 0.008797367341674467, 7.564443573293556e-05, 0.024383979420044333, 0.02788410790756546, 0.021374008158427937, 0.0036953803742704183, 0.013104935266165296, 0.002671114966841137, 0.009159016248525886, 0.005064956945909693, 0.003681503103729278, 0.0009758561877718508, 0.04040161906291373, 0.0004948154900136226, 0.04624146511169147, 0.006594572334752913, 0.003143832522642165, 0.007757875588931151, 0.0030850898821545014, 0.027203566823009617, 0.01721978081614073, 0.0029359570317655237, 0.01059502437128057, 0.006236364658167459, 0.004946834269317746, 0.0032451574275564887, 0.016990675372012154, 0.004935140406089111, 0.0006326108410349418, 0.0036785177995684037, 0.0017698129491926506, 7.475397610190448e-05, 0.0032981939769008983, 0.014765189588278771, 0.012874838822510615, 0.0024783407934703128, 0.006636389782331609, 0.003974376946173419, 0.025291230241184626]
[0, 1, 2, 3, 2, 3, 4, 5, 6, 6, 0, 9, 10, 1, 11, 4, 11, 10, 8, 15, 10, 5, 9, 22, 10, 20, 20, 22, 5, 14, 24, 15, 5, 20, 3, 17, 29, 15, 24, 0, 13, 27, 40, 37, 37, 34, 21, 15, 6, 19, 8, 1, 32, 6, 43, 52, 56, 45, 49, 57, 13, 3, 22, 24, 54, 24, 34, 35, 5, 62, 41, 31, 59, 34, 71, 49, 47, 7, 42, 61, 16, 20, 58, 60, 40, 85, 57, 40, 19, 71, 31, 83, 73, 35, 5, 91, 42, 29]

您的输入是什么?您希望您的代码对该输入做什么?共享示例输入的预期输出。好的,我现在将编辑问题以显示这一点