我正在制作一个列表,里面嵌套了一个列表。我需要迭代的第二个列表,并为一个位置赋值。所以我写了这个
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.append([[x,y],1]) #A grid is made, with a spare element
for i in range(100):
matrix[i][1]=list #The grid is two elements, co-ords...
#and a list of attributes
board()
for i in range(100):
matrix[i][2][0]=i #The first attribute,(in 2nd of2elements),
print(matrix) ##SHOULD be its own position
此代码的所有变体都会产生相同的结果:
错误或数字“99”被放置在每个实例中 地点。也就是说我的最终FOR循环似乎是分配“99” 不是我......
我尝试了几种变体(在FOR循环中生成数字是我的偏好,唉)我花了大约6个小时盯着这个。 我的所有逻辑告诉我,计算机应该知道我希望第二个元素的第一个元素是0,1,2 ... 我知道这不是因为语言的限制,所以请告诉我我没看到的内容。
答案 0 :(得分:1)
matrix[i][1]=list
执行此操作时,矩阵阵列中将分配相同的数组。因此,如果您更改一个项目,则所有项目都会更改。而是像这样分配列表的副本:
matrix[i][1] = list[:]
正如@Bakuriu所指出的,在python 3中你可以使用list.copy()方法。在Python 2中,您可以使用copy
模块复制带有l2 = copy.copy(l1)
此外,您应该更改变量'list'的名称,因为'list'是python中的内置类型。
答案 1 :(得分:0)
您应该将matrix[i][2][0]=i
提升为IndexError
,以取代for i in range(100):
matrix[i][1][0]=i #The first attribute,(in 2nd of2elements),
print(matrix) ##SHOULD be its own position
:
<div class="navbar navbar-inverse">
<!-- Header -->
<div class="navbar-header">
<button type="button" class="navbar-toggle"
data-toggle="collapse"
data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">
Bootstrap 3 Skeleton
</a>
</div>
<!-- Navbar Links -->
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="dropdown full-width">
<a href="#" class="dropdown-toggle"
data-toggle="dropdown">
Full Width <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Long
</a>
</li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
<li class="divider"></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>
<li class="dropdown full-width">
<a href="#" class="dropdown-toggle"
data-toggle="dropdown">
Full Width <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Long
</a>
</li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
<li class="divider"></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>
<li class="dropdown full-width">
<a href="#" class="dropdown-toggle"
data-toggle="dropdown">
Full Width <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Really Long
</a>
</li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
<li class="divider"></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>
</ul>
</div>
</div>
答案 2 :(得分:0)
列表在for循环中更快,但在这里你有解决方案与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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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]}
你可以在这里使用numpy,快速索引(cython memoryview更快,所以也看看它):
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)