リストは、ループのために高速ですが、ここであなたはdictsと解決策を持っている:
from pprint import pprint as ppr
matrix={}
list=[0,1,2,3,4,5,6,7,8,9]
def board():
for x in range(10):
for y in range(10):
matrix[(x,y)]=1
for k in matrix.keys():
matrix[k]=list
board()
ppr(matrix)
for k in matrix.iterkeys():
v=matrix[k][:]
v[2]=k[0]*10+k[1]
matrix[k]=v
ppr(matrix)
は入力:
{(0, 0): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
(0, 1): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
(0, 2): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
(0, 3): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
(0, 4): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
(0, 5): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
(0, 6): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
(0, 7): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
(0, 8): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
(0, 9): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
(1, 0): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
(1, 1): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
(1, 2): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
(1, 3): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
(1, 4): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
(1, 5): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
(1, 6): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
(1, 7): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
(1, 8): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
(1, 9): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
(2, 0): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
(2, 1): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
(2,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],
(0, 5): [0, 1, 5, 3, 4, 5, 6, 7, 8, 9],
(0, 6): [0, 1, 6, 3, 4, 5, 6, 7, 8, 9],
(0, 7): [0, 1, 7, 3, 4, 5, 6, 7, 8, 9],
(0, 8): [0, 1, 8, 3, 4, 5, 6, 7, 8, 9],
(0, 9): [0, 1, 9, 3, 4, 5, 6, 7, 8, 9],
(1, 0): [0, 1, 10, 3, 4, 5, 6, 7, 8, 9],
(1, 1): [0, 1, 11, 3, 4, 5, 6, 7, 8, 9],
(1, 2): [0, 1, 12, 3, 4, 5, 6, 7, 8, 9],
(1, 3): [0, 1, 13, 3, 4, 5, 6, 7, 8, 9],
(1, 4): [0, 1, 14, 3, 4, 5, 6, 7, 8, 9],
(1, 5): [0, 1, 15, 3, 4, 5, 6, 7, 8, 9],
(1, 6): [0, 1, 16, 3, 4, 5, 6, 7, 8, 9],
(1, 7): [0, 1, 17, 3, 4, 5, 6, 7, 8, 9],
(1, 8): [0, 1, 18, 3, 4, 5, 6, 7, 8, 9],
(1, 9): [0, 1, 19, 3, 4, 5, 6, 7, 8, 9],
(2, 0): [0, 1, 20, 3, 4, 5, 6, 7, 8, 9],
(2, 1): [0, 1, 21, 3, 4, 5, 6, 7, 8, 9],
(2, 2): [0, 1, 22, 3, 4, 5, 6, 7, 8, 9],
(2, 3): [0, 1, 23, 3, 4, 5, 6, 7, 8, 9],
(2, 4): [0, 1, 24, 3, 4, 5, 6, 7, 8, 9],
(2, 5): [0, 1, 25, 3, 4, 5, 6, 7, 8, 9],
(2, 6): [0, 1, 26, 3, 4, 5, 6, 7, 8, 9],
(2, 7): [0, 1, 27, 3, 4, 5, 6, 7, 8, 9],
(2, 8): [0, 1, 28, 3, 4, 5, 6, 7, 8, 9],
(2, 9): [0, 1, 29, 3, 4, 5, 6, 7, 8, 9],
(3, 0): [0, 1, 30, 3, 4, 5, 6, 7, 8, 9],
(3, 1): [0, 1, 31, 3, 4, 5, 6, 7, 8, 9],
(3, 2): [0, 1, 32, 3, 4, 5, 6, 7, 8, 9],
(3, 3): [0, 1, 33, 3, 4, 5, 6, 7, 8, 9],
(3, 4): [0, 1, 34, 3, 4, 5, 6, 7, 8, 9],
(3, 5): [0, 1, 35, 3, 4, 5, 6, 7, 8, 9],
(3, 6): [0, 1, 36, 3, 4, 5, 6, 7, 8, 9],
(3, 7): [0, 1, 37, 3, 4, 5, 6, 7, 8, 9],
(3, 8): [0, 1, 38, 3, 4, 5, 6, 7, 8, 9],
(3, 9): [0, 1, 39, 3, 4, 5, 6, 7, 8, 9],
(4, 0): [0, 1, 40, 3, 4, 5, 6, 7, 8, 9],
(4, 1): [0, 1, 41, 3, 4, 5, 6, 7, 8, 9],
(4, 2): [0, 1, 42, 3, 4, 5, 6, 7, 8, 9],
(4, 3): [0, 1, 43, 3, 4, 5, 6, 7, 8, 9],
(4, 4): [0, 1, 44, 3, 4, 5, 6, 7, 8, 9],
(4, 5): [0, 1, 45, 3, 4, 5, 6, 7, 8, 9],
(4, 6): [0, 1, 46, 3, 4, 5, 6, 7, 8, 9],
(4, 7): [0, 1, 47, 3, 4, 5, 6, 7, 8, 9],
(4, 8): [0, 1, 48, 3, 4, 5, 6, 7, 8, 9],
(4, 9): [0, 1, 49, 3, 4, 5, 6, 7, 8, 9],
(5, 0): [0, 1, 50, 3, 4, 5, 6, 7, 8, 9],
(5, 1): [0, 1, 51, 3, 4, 5, 6, 7, 8, 9],
(5, 2): [0, 1, 52, 3, 4, 5, 6, 7, 8, 9],
(5, 3): [0, 1, 53, 3, 4, 5, 6, 7, 8, 9],
(5, 4): [0, 1, 54, 3, 4, 5, 6, 7, 8, 9],
(5, 5): [0, 1, 55, 3, 4, 5, 6, 7, 8, 9],
(5, 6): [0, 1, 56, 3, 4, 5, 6, 7, 8, 9],
(5, 7): [0, 1, 57, 3, 4, 5, 6, 7, 8, 9],
(5, 8): [0, 1, 58, 3, 4, 5, 6, 7, 8, 9],
(5, 9): [0, 1, 59, 3, 4, 5, 6, 7, 8, 9],
(6, 0): [0, 1, 60, 3, 4, 5, 6, 7, 8, 9],
(6, 1): [0, 1, 61, 3, 4, 5, 6, 7, 8, 9],
(6, 2): [0, 1, 62, 3, 4, 5, 6, 7, 8, 9],
(6, 3): [0, 1, 63, 3, 4, 5, 6, 7, 8, 9],
(6, 4): [0, 1, 64, 3, 4, 5, 6, 7, 8, 9],
(6, 5): [0, 1, 65, 3, 4, 5, 6, 7, 8, 9],
(6, 6): [0, 1, 66, 3, 4, 5, 6, 7, 8, 9],
(6, 7): [0, 1, 67, 3, 4, 5, 6, 7, 8, 9],
(6, 8): [0, 1, 68, 3, 4, 5, 6, 7, 8, 9],
(6, 9): [0, 1, 69, 3, 4, 5, 6, 7, 8, 9],
(7, 0): [0, 1, 70, 3, 4, 5, 6, 7, 8, 9],
(7, 1): [0, 1, 71, 3, 4, 5, 6, 7, 8, 9],
(7, 2): [0, 1, 72, 3, 4, 5, 6, 7, 8, 9],
(7, 3): [0, 1, 73, 3, 4, 5, 6, 7, 8, 9],
(7, 4): [0, 1, 74, 3, 4, 5, 6, 7, 8, 9],
(7, 5): [0, 1, 75, 3, 4, 5, 6, 7, 8, 9],
(7, 6): [0, 1, 76, 3, 4, 5, 6, 7, 8, 9],
(7, 7): [0, 1, 77, 3, 4, 5, 6, 7, 8, 9],
(7, 8): [0, 1, 78, 3, 4, 5, 6, 7, 8, 9],
(7, 9): [0, 1, 79, 3, 4, 5, 6, 7, 8, 9],
(8, 0): [0, 1, 80, 3, 4, 5, 6, 7, 8, 9],
(8, 1): [0, 1, 81, 3, 4, 5, 6, 7, 8, 9],
(8, 2): [0, 1, 82, 3, 4, 5, 6, 7, 8, 9],
(8, 3): [0, 1, 83, 3, 4, 5, 6, 7, 8, 9],
(8, 4): [0, 1, 84, 3, 4, 5, 6, 7, 8, 9],
(8, 5): [0, 1, 85, 3, 4, 5, 6, 7, 8, 9],
(8, 6): [0, 1, 86, 3, 4, 5, 6, 7, 8, 9],
(8, 7): [0, 1, 87, 3, 4, 5, 6, 7, 8, 9],
(8, 8): [0, 1, 88, 3, 4, 5, 6, 7, 8, 9],
(8, 9): [0, 1, 89, 3, 4, 5, 6, 7, 8, 9],
(9, 0): [0, 1, 90, 3, 4, 5, 6, 7, 8, 9],
(9, 1): [0, 1, 91, 3, 4, 5, 6, 7, 8, 9],
(9, 2): [0, 1, 92, 3, 4, 5, 6, 7, 8, 9],
(9, 3): [0, 1, 93, 3, 4, 5, 6, 7, 8, 9],
(9, 4): [0, 1, 94, 3, 4, 5, 6, 7, 8, 9],
(9, 5): [0, 1, 95, 3, 4, 5, 6, 7, 8, 9],
(9, 6): [0, 1, 96, 3, 4, 5, 6, 7, 8, 9],
(9, 7): [0, 1, 97, 3, 4, 5, 6, 7, 8, 9],
(9, 8): [0, 1, 98, 3, 4, 5, 6, 7, 8, 9],
(9, 9): [0, 1, 99, 3, 4, 5, 6, 7, 8, 9]}
あなたは、高速インデックス(速くはそうあまりにもそれを見cython memoryviewである)で、ここでnumpyの使用することができます
matrix=np.zeros((100,10),dtype=int)
list=[0,1,2,3,4,5,6,7,8,9]
def board(matrix):
for x in range(10):
for y in range(10):
matrix[x*10+y]=1 #but here You populate whole sub-array of shape (10) with 1, so it will be: [1,1,1,1...,1]
print matrix
for k in range(matrix.shape[0]):
matrix[k]=list
board(matrix)
ppr(matrix)
for k in range(matrix.shape[0]):
matrix[k,2]=k
ppr(matrix)
が**使用は、内蔵しない変数名などの名前。 'numbers = [...]'のように、あなたの変数に 'list'とは違う名前を使うことをお勧めします。 – Bakuriu
xyループのif文を使って、行列要素に 'list'を割り当てることができますか? –
さらに、 'matrix [i] [2] [0] = i'は、あなたの行列が2つの要素のリストで埋められているので' IndexError'を発生させます... – mvelay