Python 如何在列表中附加不同的返回值
我有一个函数,类似于:Python 如何在列表中附加不同的返回值,python,list,Python,List,我有一个函数,类似于: def function(self): matrix = [] for i in range(10): contents = [1,0,1,0] #integer values in a list, changes every time when running function() matrix .append(contents) self.matrix = matrix return matrix
def function(self):
matrix = []
for i in range(10):
contents = [1,0,1,0] #integer values in a list, changes every time when running function()
matrix .append(contents)
self.matrix = matrix
return matrix
我还有一个空列表,我想做的是将matrix
添加到empty\u matrix
5次,
所以结果应该是这样的:
empty_matrix = [[matrix1],[matrix2],[matrix3],[matrix4],[matrix5]]
但是我不知道如何使矩阵1,2,3,4,5有不同的值。当我单独运行函数(self)
时,它会返回不同的值,但当我将其附加到空矩阵中时,它会附加相同的值,如
empty_matrix = [[matrix1],[matrix1],[matrix1],[matrix1],[matrix1]].
任何帮助都将不胜感激
原始代码如下:
def generate_population(self):
model = self.model
weights_origin = model.get_weights()
shape_list = []
matrix= []
empty_matrix= [[],[],[],[],[]]
for i, w in enumerate(weights_origin):
#get shape of w -> save shape into shape_list -> generate binary_value that has the shape of shape_list
#-> convert binary_value to float32 -> define it as tf Variable -> append to rand_covers list
w_shape = tf.shape(w)
shape_list.append(w_shape)
binary_value = np.random.randint(2, size = shape_list[i])
to_float = tf.cast(binary_value, tf.float32)
weights2 = tf.Variable(to_float, name="addition_"+str(i), trainable=True)
matrix.append(weights2)
for j in range(5):
empty_matrix[j].append(matrix)
self.matrix = matrix
return matrix
而不是
for j in range(5):
empty_matrix[j].append(matrix)
你需要
for j in range(5):
matrix = function()
empty_matrix[j].append(matrix)
如果您希望在开始时使用相同的矩阵,并在以后更改其中一个矩阵中的值而不更改其他矩阵中的值,则必须使用
.copy()
或copy.deepcopy()
用于深度复制
import copy
matrix = function()
for j in range(5):
empty_matrix[j].append( copy.deepcopy(matrix.copy) )
编辑: 我不明白您试图做什么,但代码似乎很奇怪,我会写它
def generate_population(self):
model = self.model
weights_origin = model.get_weights()
shape_list = []
empty_matrix= [[],[],[],[],[]]
for j in range(5):
matrix = []
for i, w in enumerate(weights_origin):
#get shape of w -> save shape into shape_list -> generate binary_value that has the shape of shape_list
#-> convert binary_value to float32 -> define it as tf Variable -> append to rand_covers list
w_shape = tf.shape(w)
shape_list.append(w_shape)
binary_value = np.random.randint(2, size = shape_list[i])
to_float = tf.cast(binary_value, tf.float32)
weights2 = tf.Variable(to_float, name="addition_"+str(i), trainable=True)
matrix.append(weights2)
empty_matrix[j].append(matrix)
return empty_matrix
# --- outside function ---
empty_matrix = generate_population()
或
请您添加整个代码,以便我们重现您的问题?您如何将矩阵添加到
空_矩阵
中?为什么只在中为范围(5)中的j运行append()?通过这种方式,您可以将相同的矩阵添加5次。如果你必须运行生成新矩阵的函数,你能解释一下矩阵=函数()是如何工作的吗?我按照你的指示做了,但上面说“函数名”没有定义我不明白-在这个问题上,你显示def function()
,它返回矩阵
,当你运行matrix=function()
时,它会给你这个矩阵,如果你没有function()
那么就代替matrix=function())
您必须运行生成新矩阵的代码。抱歉,仍然无法获取它。我不认为matrix=generate_population()在main函数中起作用,而在generate_population本身中起作用。我想我遗漏了一些东西。我不知道你到底想在函数中生成什么——一个矩阵或矩阵列表(空矩阵),但我会用不同的方式来写。请参阅答案中的新代码。
def generate_population(self):
model = self.model
weights_origin = model.get_weights()
shape_list = []
empty_matrix= [[],[],[],[],[]]
for j in range(5):
matrix = []
for i, w in enumerate(weights_origin):
#get shape of w -> save shape into shape_list -> generate binary_value that has the shape of shape_list
#-> convert binary_value to float32 -> define it as tf Variable -> append to rand_covers list
w_shape = tf.shape(w)
shape_list.append(w_shape)
binary_value = np.random.randint(2, size = shape_list[i])
to_float = tf.cast(binary_value, tf.float32)
weights2 = tf.Variable(to_float, name="addition_"+str(i), trainable=True)
matrix.append(weights2)
empty_matrix[j].append(matrix)
return empty_matrix
# --- outside function ---
empty_matrix = generate_population()
def generate_population(self):
model = self.model
weights_origin = model.get_weights()
shape_list = []
matrix = []
for i, w in enumerate(weights_origin):
#get shape of w -> save shape into shape_list -> generate binary_value that has the shape of shape_list
#-> convert binary_value to float32 -> define it as tf Variable -> append to rand_covers list
w_shape = tf.shape(w)
shape_list.append(w_shape)
binary_value = np.random.randint(2, size=shape_list[i])
to_float = tf.cast(binary_value, tf.float32)
weights2 = tf.Variable(to_float, name="addition_"+str(i), trainable=True)
matrix.append(weights2)
return matrix
# --- outside function ---
empty_matrix= [[],[],[],[],[]]
for j in range(5):
matrix = generate_population()
empty_matrix[j].append(matrix)