为什么它在第1行给出了一个不同的向量,为所有其他行给出了相同的向量?
for i in range(0,c_number):
for i in range(0,len(s_name)):
if randint(1,101)>70:
children[i] = alfa[randint(0,26)]
CM.append([children])
print(children)
children=parent
CM=np.vstack(CM)
这是我在以下时间打印CM和孩子时的结果:
['u', 's', 'y', 'h', 'l', 'g', 'e', 'd']
['u', 'w', 'h', 'c', 'h', 'g', 'n', 'b']
['u', 'k', 'h', 'c', 'h', 'g', 'n', 'b']
['u', 'i', 'h', 'i', 'h', 'g', 'j', 'b']
['u', 'c', 'h', 'y', 'h', 'g', 'j', 'b']
['u', 'v', 'j', 'r', 'h', 'g', 'd', 'b']
['y', 'v', 'j', 'r', 'h', 'g', 'd', 'b']
['y', 'v', 'j', 'r', 'h', 'g', 'd', 'b']
['y', 'n', 'j', 'f', 'o', 'q', 'd', 'b']
['v', 'n', 'j', 'f', 'o', 'q', 'd', 'b']
[['u' 's' 'y' 'h' 'l' 'g' 'e' 'd']
['v' 'n' 'j' 'f' 'o' 'q' 'd' 'b']
['v' 'n' 'j' 'f' 'o' 'q' 'd' 'b']
['v' 'n' 'j' 'f' 'o' 'q' 'd' 'b']
['v' 'n' 'j' 'f' 'o' 'q' 'd' 'b']
['v' 'n' 'j' 'f' 'o' 'q' 'd' 'b']
['v' 'n' 'j' 'f' 'o' 'q' 'd' 'b']
['v' 'n' 'j' 'f' 'o' 'q' 'd' 'b']
['v' 'n' 'j' 'f' 'o' 'q' 'd' 'b']
['v' 'n' 'j' 'f' 'o' 'q' 'd' 'b']]
答案 0 :(得分:3)
您需要在两个不同的循环中更改i
两次,使第二个循环具有不同的变量,如j
或其他
#---v
for i in range(0,c_number):
for i in range(0,len(s_name)):
#----^
答案 1 :(得分:0)
执行时:
children=parent
它使children
引用与parent
相同的对象(列表),并在所有后续迭代中重复使用。
这是另一个例子:
parent = []
children = parent
print(parent is children) # Prints True because they are the _same_ object
parent.append('parent')
print(parent) # Prints ['parent']
print(children) # Also prints ['parent']
答案 2 :(得分:0)
弄清楚:
for j in range(0,c_number):
for i in range(0,len(s_name)):
if randint(1,101)>70:
children[i] = alfa[randint(0,26)]
CM.append([children])
print(children)
print(parent)
children=parent[:]
这使我只从父母那里获取信息