網站首頁 編程語言 正文
01?示例函數
1.1 代碼及結果
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函數
在代碼中,包括有以下幾個函數值得進一步的探究,以備之后學習和應用。
- np.index_exp:產生array 的索引元組;
- shape() + (3,)?: 對于一個元組增加維度;
- 省略號: 自適應數組索引;
語法糖?(Syntactic Sugar)是為了方便編程人員使用的變化的語法,它并不對原來的功能產生任何影響。
比如:
- a[i] : *(a+i)
- a[i][j] :?(a+icol +j)
02?數組索引
2.1 省略號
利用省略號,可以自適應匹配前面省略的數組索引。
下面定義了一個3D數字: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 擴增數組維度
擴增數組維度,可以使用一下兩個等效的語法來完成。
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 元組相加
元組可以通過“+”串聯在一起:
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]]
總結
在Python中還存在一些有趣的 Syntatic Sugar (語法糖果),在編程的時候可以進一步簡化編程的效率。
原文鏈接:https://blog.csdn.net/zhuoqingjoking97298/article/details/122866323
相關推薦
- 2022-04-09 c語言循環加數組實現漢諾塔問題_C 語言
- 2022-06-02 python套接字socket通信_python
- 2022-10-04 Linux行處理工具之grep?正則表達式詳解_正則表達式
- 2022-03-07 Android顯示系統SurfaceFlinger分析_Android
- 2024-03-05 git創建分支
- 2022-11-17 React中實現插槽效果的方案詳解_React
- 2022-07-03 Golang之空結構體和零長數組的實踐
- 2022-05-03 SQL?Server查詢某個字段在哪些表中存在_MsSql
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支