Add an object to a python list

后端 未结 4 1595
傲寒
傲寒 2021-01-07 17:38

I am trying to add an object to a list but since I\'m adding the actual object when I try to reset the list thereafter, all the values in the list are reset. Is there an act

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-07 17:59

    If i am correct in believing that you are adding a variable to the array but when you change that variable outside of the array, it also changes inside the array but you don't want it to then it is a really simple solution.

    When you are saving the variable to the array you should turn it into a string by simply putting str(variablename). For example:

    array.append(str(variablename))
    

    Using this method your code should look like this:

    arrayList = []
    for x in allValues:
        result = model(x)
        arrayList.append(str(wM))        #this is the only line that is changed.
        wM.reset()
    

提交回复
热议问题