網(wǎng)站首頁 編程語言 正文
01?示例函數(shù)
1.1 代碼及結(jié)果
import matplotlib.pyplot as plt import matplotlib.colors import numpy as np from mpl_toolkits.mplot3d import Axes3D def midpoints(x): sl = () for i in range(x.ndim): x = (x[sl + np.index_exp[:-1]] + x[sl + np.index_exp[1:]]) / 2.0 sl += np.index_exp[:] return x # prepare some coordinates, and attach rgb values to each r, theta, z = np.mgrid[0:1:11j, 0:np.pi*2:25j, -0.5:0.5:11j] x = r*np.cos(theta) y = r*np.sin(theta) rc, thetac, zc = midpoints(r), midpoints(theta), midpoints(z) # define a wobbly torus about [0.7, *, 0] sphere = (rc - 0.7)**2 + (zc + 0.2*np.cos(thetac*2))**2 < 0.2**2 # combine the color components hsv = np.zeros(sphere.shape + (3,)) hsv[..., 0] = thetac / (np.pi*2) hsv[..., 1] = rc hsv[..., 2] = zc + 0.5 colors = matplotlib.colors.hsv_to_rgb(hsv) # and plot everything fig = plt.figure() ax = fig.gca(projection='3d') ax.voxels(x, y, z, sphere, facecolors=colors, edgecolors=np.clip(2*colors - 0.5, 0, 1), # brighter linewidth=0.5) plt.show()
繪制的3D圖像
1.2 Python函數(shù)
在代碼中,包括有以下幾個函數(shù)值得進一步的探究,以備之后學(xué)習(xí)和應(yīng)用。
- np.index_exp:產(chǎn)生array 的索引元組;
- shape() + (3,)?: 對于一個元組增加維度;
- 省略號: 自適應(yīng)數(shù)組索引;
語法糖?(Syntactic Sugar)是為了方便編程人員使用的變化的語法,它并不對原來的功能產(chǎn)生任何影響。
比如:
- a[i] : *(a+i)
- a[i][j] :?(a+icol +j)
02?數(shù)組索引
2.1 省略號
利用省略號,可以自適應(yīng)匹配前面省略的數(shù)組索引。
下面定義了一個3D數(shù)字:x。
import sys,os,math,time import matplotlib.pyplot as plt from numpy import * x = array([[[1],[2],[3]], [[4],[5],[6]]]) print("x: {}".format(x), "x.shape: {}".format(x.shape))
x: [[[1] [2] [3]] [[4] [5] [6]]] x.shape: (2, 3, 1)
下面通過省略號訪問x,可以看到它與前面補齊索引是相同的效果。
x1 = x[...,0] x2 = x[:,:,0] print("x1: {}".format(x1),"x2: {}".format(x2))
x1.shape: (2, 1, 3, 1) x2.shape: (2, 1, 3, 1)
2.2 擴增數(shù)組維度
擴增數(shù)組維度,可以使用一下兩個等效的語法來完成。
x1 = x[:,None,:,:] x2 = x[:,newaxis,:,:] print("x1.shape: {}".format(x1.shape), "x2.shape: {}".format(x2.shape))
x1.shape: (2, 1, 3, 1) x2.shape: (2, 1, 3, 1)
2.3 元組相加
元組可以通過“+”串聯(lián)在一起:
a = (1,2,3) b = (1,) print(a+b)
(1, 2, 3, 1)
實際上對于列表也是可以的:
a = [1,2,3] b = [1] print(a+b)
[1, 2, 3, 1]
但是list 與 tuple 不能夠疊加:
a = [1,2,3] b = (1,) print(a+b)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_164/1922126339.py in <module> 5 a = [1,2,3] 6 b = (1,) ----> 7 printt(a+b) TypeError: can only concatenate list (not "tuple") to list
2.4 一維變二維
import numpy a = array([1,2,3,4]) b = array([5,6,7,8]) d = numpy.r_[a,b] print("d: {}".format(d))
d: [1 2 3 4 5 6 7 8]?
import numpy a = array([1,2,3,4]) b = array([5,6,7,8]) d = numpy.c_[a,b] print("d: {}".format(d))
d: [[1 5]
?[2 6]
?[3 7]
?[4 8]]
總結(jié)
在Python中還存在一些有趣的 Syntatic Sugar (語法糖果),在編程的時候可以進一步簡化編程的效率。
原文鏈接:https://blog.csdn.net/zhuoqingjoking97298/article/details/122866323
相關(guān)推薦
- 2022-03-30 Python?使用和高性能技巧操作大全_python
- 2022-04-18 create-react-app 中支持sass,怎么搞?
- 2023-01-28 python基礎(chǔ)之reverse和reversed函數(shù)的介紹及使用_python
- 2022-11-17 Android對話框AlertDialog與DatePickerDialog及TimePickerD
- 2023-01-12 Golang遠程調(diào)用框架RPC的具體使用_Golang
- 2023-01-06 使用find命令快速定位配置文件位置_linux shell
- 2022-05-12 el-table滾動懶加載,顯示加載狀態(tài)
- 2021-12-04 淺談C++中const與constexpr的區(qū)別_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之認證信息的處理
- Spring Security之認證過濾器
- 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同步修改后的遠程分支