python中数组追加数据_将数组追加到数组

@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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值