網(wǎng)站首頁 編程語言 正文
1. 二維(多維)數(shù)組降為一維數(shù)組
方法1: reshape()+concatenate 函數(shù),
這個方法是間接法,利用 reshape() 函數(shù)的屬性,間接的把二維數(shù)組轉(zhuǎn)換為一維數(shù)組;
import numpy as np mulArrays = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(list(np.concatenate(mulArrays.reshape((-1, 1), order="F")))) Out[1]: [1, 4, 7, 2, 5, 8, 3, 6, 9]
方法2: flatten() 函數(shù),
推薦使用這個方法,這個方法是 numpy
自帶的函數(shù);
# coding = utf-8 import numpy as np import random # 把二維數(shù)組轉(zhuǎn)換為一維數(shù)組 t1 = np.arange(12) print(t1) Out[0]: [ 0 1 2 3 4 5 6 7 8 9 10 11] t2 = t1.reshape(3, 4) print(t2) t3 = t2.reshape(t2.shape[0] * t2.shape[1], ) print(t3) t4 = t2.flatten() print(t4)
運行效果如下圖所示:
可以看到這兩種方式都可以把二維數(shù)組轉(zhuǎn)換為一維數(shù)組,但是推薦使用 flatten()
函數(shù),該方法也可以將多維數(shù)組轉(zhuǎn)換為一維數(shù)組。
import numpy as np a = np.array([[1, 2], [3, 4], [9, 8]]) b = a.flatten() print(b)
輸出結(jié)果為:[1, 2, 3, 4, 9, 8]
方法3: itertools.chain
import numpy as np a = np.array([[1, 2], [3, 4], [9, 8]]) # 使用庫函數(shù) from itertools import chain a_a = list(chain.from_iterable(a)) print(a_a)
輸出結(jié)果為:[1, 2, 3, 4, 9, 8]
方法4: sum()
mulArrays = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] print(sum(mulArrays, [])) # [1, 2, 3, 4, 5, 6, 7, 8, 9]
方法5:operator.add + reduce
import operator from functools import reduce mulArrays = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] print(reduce(operator.add, mulArrays)) # [1, 2, 3, 4, 5, 6, 7, 8, 9]
方法6:列表推導(dǎo)式
mulArrays = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] print([i for arr in mulArrays for i in arr]) # [1, 2, 3, 4, 5, 6, 7, 8, 9]
2. 一維數(shù)組升為 2 維數(shù)組
方法1:numpy 方法
利用函數(shù) reshape
或者是 resize
。
使用 reshape
的時候需要注意 reshape
的結(jié)果不改變,因此適用于還要用到原數(shù)組的情況。
使用 resize
會改變原數(shù)組,因此適用于一定需要修改后的結(jié)果為值的情況。
import numpy as np x = np.arange(20) # 生成數(shù)組 print(x) result = x.reshape((4, 5)) # 將一維數(shù)組變成4行5列 原數(shù)組不會被修改或者覆蓋 x.resize((2, 10)) # 覆蓋原來的數(shù)據(jù)將新的結(jié)果給原來的數(shù)組 print(x)
輸出結(jié)果
[ 0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 10 11 12 13 14 15 16 17 18 19]
[[ 0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9]
?[10 11 12 13 14 15 16 17 18 19]]
總結(jié)
原文鏈接:https://blog.csdn.net/weixin_46713695/article/details/126725305
相關(guān)推薦
- 2023-04-03 Input系統(tǒng)分發(fā)策略及其應(yīng)用示例詳解_Android
- 2022-09-17 C++?中的異常拋出和捕獲方式_C 語言
- 2023-03-25 Python?flask?框架使用flask-login?模塊的詳細過程_python
- 2022-11-09 Android?使用maven?publish插件發(fā)布產(chǎn)物(aar)流程實踐_Android
- 2022-04-04 css:動畫 小米官網(wǎng)盒子陰影 心跳動畫
- 2023-07-27 react中使用echarts
- 2022-11-26 Python反向傳播實現(xiàn)線性回歸步驟詳細講解_python
- 2023-01-09 使用C#?11的靜態(tài)接口方法改進?面向約定?的設(shè)計方法_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支