如何在单个数组索引中存储列表

时间:2017-02-16 11:42:01

标签: arrays python-2.7 list indexing

如何在单个数组索引中存储完整列表?

ip[x]=[1,2,3,1,2,3,3,2,1,1]

def fitness(c)
      ..
      ..
      return

for x in range(0, 100):
    print 'chromosome%d'%(x+1)
    c=[randint(1,3) for y in range(10)]
    fitness(c)
    ip[x]=c

1 个答案:

答案 0 :(得分:0)

我真的不明白你想要什么。正如Willem在评论中告诉你的那样,你可以在阵列中存储任何东西。你想做那样的事吗?

import numpy as np

L = []

for x in range(11):
    c=[np.random.randint(1,4) for y in range(10)]
    L.append(c)

array = np.array(L)

print array

结果如下:

[[1 2 1 1 2 1 2 3 1 2]
 [2 1 2 3 1 3 1 3 3 2]
 [3 3 2 3 1 2 2 1 2 3]
 [1 3 1 1 1 1 1 2 2 1]
 [2 2 2 2 3 1 2 1 3 2]
 [1 3 1 1 1 2 3 1 3 3]
 [1 3 3 3 2 3 3 2 2 2]
 [3 2 1 3 2 3 1 1 1 3]
 [3 1 1 2 1 1 1 2 2 2]
 [2 3 2 2 3 2 3 2 1 3]
 [1 2 2 3 3 1 3 3 1 1]]

编辑:或类似的东西?

import numpy as np

L1 = []

L2 = []


for j in range(3):
    for i in range(3):
        c = [np.random.randint(1,4) for y in range(10)]
        L2.append(c)
    L1.append(L2)
    L2 = []

print np.array(L1)

结果如下:

[[[1 3 3 2 1 3 2 1 1 3]
  [3 2 2 1 1 1 2 3 3 1]
  [1 2 3 3 1 1 3 1 1 2]]

 [[3 1 1 1 2 2 2 1 2 1]
  [1 3 3 3 1 1 1 1 1 2]
  [2 3 1 1 1 2 1 1 1 3]]

 [[1 3 3 1 3 2 3 1 2 3]
  [3 3 2 3 3 3 1 2 3 2]
  [2 3 3 2 2 1 3 1 1 3]]]