python - 如何扩展numpy数组
问题描述
我有 2 个 numpy 数组,我想使用扩展将这两个数组组合在一起。例如:
a = [[1,2,3],[4,5,6],[7,8,9]]
b = [[0,0,0],[1,1,1]]
我想要的是
c = [[1,2,3],[4,5,6],[7,8,9],[0,0,0],[1,1,1]]
看来我不能extend
用作 python 列表。否则会引发AttributeError: 'numpy.ndarray' object has no attribute 'extend'
错误。
目前我尝试将它们转换为列表:
a_list = a.tolist()
b_list = b.tolist()
a_list.extend(b_list)
c = numpy.array(a_list)
我想知道是否存在更好的解决方案?
解决方案
利用 -
np.concatenate((a, b), axis=0)
或者 -
np.vstack((a,b))
或者 -
a.append(b) # appends in-place, a will get modified directly
输出
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[0, 0, 0],
[1, 1, 1]])
推荐阅读
- c# - 使用配置文件指定连接字符串来定义单元测试中的数据源
- c++ - 将 void* 强制转换为具有多重继承的类
- phpstorm - 从 PhpStorm 中查找 GIT 存储库
- visual-studio-2017 - SSIS:“找不到 SQLIPackage140”
- javascript - 如何删除 lodash 模板创建的换行符?
- python - Tensorflow AttributeError:类型对象'numpy.ndarray'没有属性'__array_function__'
- ios - 重新加载 UITableView 除了它的一部分
- c# - Xamarin IOS:BLE
- mariadb - 为什么一个简单的 ALTER 语句会导致 galera 崩溃
- java - Jenkins + Maven 发布插件:release:branch + Git - 无法提交文件