【问题标题】:Join 2 randomly genreted list加入2个随机生成的列表
【发布时间】:2021-07-22 07:07:21
【问题描述】:

我正在尝试将 2 个随机生成的列表合并为一个,但它会明智地添加它们。假设我生成了 2 个列表,每个列表有 2 个随机数,我希望我的输出列表是数字。例如:

import numpy as np

x1 = np.random.uniform(0,0.1, 2)
x2 = np.random.uniform(0,0.1, 2)

x = x1 + x2

print(x1)
print(x2)
print(x)
The output is: 
[0.06878713 0.03807816]
[0.01801809 0.06292975]
[0.08680523 0.10100791]

But I want my output as 
[0.06878713 0.03807816]
[0.01801809 0.06292975]
[0.06878713 0.03807816 0.01801809 0.06292975]

如果我使用 append() 或 extend() 它给了我:AttributeError: 'numpy.ndarray' object has no attribute 'append'。

【问题讨论】:

  • 问题是你认为它们是列表,但它们是 numpy 数组。

标签: python-3.x list numpy random


【解决方案1】:

正如列出的here,您将执行以下操作:

import numpy as np

x1 = np.random.uniform(0,0.1, 2)
x2 = np.random.uniform(0,0.1, 2)

x = np.concatenate((x1, x2))

print(x1)
print(x2)
print(x)

输出是:

[0.09031488 0.0600346]
[0.03298771 0.08265562]
[0.09031488 0.0600346  0.03298771 0.08265562]

【讨论】:

    猜你喜欢
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-15
    • 2016-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多