@John对于如何使用来自numpy.append的return值是正确的,因为它不会修改原始数组。但是,您的预期输出有一个问题:[[0 1 0 0]
[1 1 0 1]
[0. 0. 0. 0.]
5]
不是一个可能的numpy数组,因为两个原因:一个是某些元素是整数,有些是浮点,但是numpy数组的数据类型必须是一致的;另一个是每行的长度不一样,但是numpy数组必须具有统一(矩形)的形状。在
我想你最好还是把这三样东西都还给你:comm_system作为整数数组
score_list作为浮点数组
和context_size作为int(不是数组)。在
你可以用元组来实现:
^{pr2}$
然后您可以像这样“解包”元组:random_agent, scores, size = new_agent('random', 5)
或者把它们放在一个元组中:random_agent_info = new_agent('random', 5)
你会有In [331]: random_agent, scores, size = new_agent('random', 5)
In [332]: random_agent
Out[332]:
array([[0, 1, 1, 0],
[0, 1, 0, 1]])
In [333]: scores
Out[333]: array([ 0., 0., 0., 0.])
In [334]: size
Out[334]: 5
In [336]: random_agent_info
Out[336]:
(array([[1, 1, 0, 1],
[0, 1, 0, 0]]),
array([ 0., 0., 0., 0.]),
5)
In [337]: random_agent_info[0]
Out[337]:
array([[1, 1, 0, 1],
[0, 1, 0, 0]])
In [338]: random_agent_info[1]
Out[338]: array([ 0., 0., 0., 0.])
In [339]: random_agent_info[2]
Out[339]: 5
如果您确实想让comm_system和score_list成为一个(3,2)数组,那么可以使用:def new_agent(agent_type, context_size):
...
return np.vstack([comm_system, score_list]), context_size
然后得到一个数组和一个int:In [341]: random_agent, size = new_agent('random', 5)
In [342]: random_agent
Out[342]:
array([[ 1., 0., 1., 1.],
[ 1., 0., 1., 0.],
[ 0., 0., 0., 0.]])
In [343]: size
Out[343]: 5