简体   繁体   English

Python应该返回更多行时仅返回2个不同的行

[英]Python returns only 2 different rows when it should return many more

Why on earth does this give me one different vector in row 1 and the same vectors for all other rows? 为什么在地球上这会给我第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)

This is currently the result when I print CM after and children during: 目前,这是在以下时间和孩子打印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']]

You need to change i you have it twice in two different loops, make the second loop have a different variable like j or something 您需要更改i在两个不同的循环中两次,使第二个循环具有不同的变量,例如j或其他内容

    #---v
    for i in range(0,c_number):
     for i in range(0,len(s_name)):
    #----^

When it executes: 执行时:

children=parent

it makes children refer to the same object (a list) as parent , which is reused in all the subsequent iterations. 它使childrenparent引用相同的对象(列表),并在所有后续迭代中重用。

Here's another example: 这是另一个例子:

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']

Figure it out: 想办法:

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[:]

This makes me only take the information from parent 这使我只从父母那里获取信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM