python常用函數(shù)解釋的簡(jiǎn)單介紹

Python常用函數(shù)三有哪些?這7個(gè)函數(shù)使用頻率最高,總算搞明白了

1.1 例如:print(hex(2))案例

成都創(chuàng)新互聯(lián)自2013年創(chuàng)立以來,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元鐵山港做網(wǎng)站,已為上家服務(wù),為鐵山港各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792

1.2 輸出函數(shù):print(hex(2))

1.3 輸出結(jié)果:0x2

1.4 解析說明:返回16進(jìn)制的數(shù)。

2.1 例如:print(chr(10))案例

2.2 輸出函數(shù):print(chr(10))

2.3 輸出結(jié)果:0o12

2.4 解析說明:返回當(dāng)前整數(shù)對(duì)應(yīng)的ASCll碼

3.1 例如:print(ord("b"))案例

3.2 輸出函數(shù):print(ord("b"))

3.3 輸出結(jié)果:98

3.4 解析說明:返回當(dāng)前ASCll碼的10進(jìn)制數(shù)

4.1 例如:print(chr(97))

4.2 輸出函數(shù):print(chr(97))

4.3 輸出結(jié)果:b

4.4 解析說明:返回當(dāng)前ASCll碼的10進(jìn)制數(shù)。

案例一:給你一個(gè)字符串,s = 'hello kitty'

1.1 輸出函數(shù):print(s.capitalize())

1.2 輸出結(jié)果:0x2

1.3 解析說明:返回16進(jìn)制的數(shù)。

2.1輸出函數(shù):print(s.replace('kitty','kuang'))

2.2 輸出結(jié)果:hello kuang

2.3 解析說明:替換功能,將kitty換成kuang。

2.4 輸出函數(shù):print(s.replace('4','KK'))

2.5 輸出結(jié)果:12KK12KK

2.6 解析說明:所有的4都替換成KK

2.7 輸出函數(shù):print(s.replace('4','KK'))

2.8 輸出結(jié)果:12KK12KK124

2.9 解析說明:將前兩個(gè)的4替換成go

案例一:給你一個(gè)字符串,ip = '192.168.1.1'

3.1 輸出函數(shù):print(ip.split(','))

3.2 輸出結(jié)果:['192.168.1.1']

3.3 解析說明:將字符串分割成列表

案例一:給你一個(gè)字符串,ip = '192.168.1.1'

3.3 輸出函數(shù):print(ip.split(',',2))

3.4 輸出結(jié)果:['192.168.1.1']

3.5 解析說明:從第二個(gè)開始分割成列表

68 個(gè) Python 內(nèi)置函數(shù)詳解

內(nèi)置函數(shù)就是Python給你提供的,拿來直接用的函數(shù),比如print.,input等。

截止到python版本3.6.2 ,python一共提供了68個(gè)內(nèi)置函數(shù),具體如下

本文將這68個(gè)內(nèi)置函數(shù)綜合整理為12大類,正在學(xué)習(xí)Python基礎(chǔ)的讀者一定不要錯(cuò)過,建議收藏學(xué)習(xí)!

(1)列表和元組

(2)相關(guān)內(nèi)置函數(shù)

(3)字符串

frozenset 創(chuàng)建一個(gè)凍結(jié)的集合,凍結(jié)的集合不能進(jìn)行添加和刪除操作。

語法:sorted(Iterable, key=函數(shù)(排序規(guī)則), reverse=False)

語法:fiter(function. Iterable)

function: 用來篩選的函數(shù). 在?lter中會(huì)自動(dòng)的把iterable中的元素傳遞給function. 然后根據(jù)function返回的True或者False來判斷是否保留留此項(xiàng)數(shù)據(jù) , Iterable: 可迭代對(duì)象

搜索公眾號(hào)頂級(jí)架構(gòu)師后臺(tái)回復(fù)“面試”,送你一份驚喜禮包。

語法 : map(function, iterable)

可以對(duì)可迭代對(duì)象中的每一個(gè)元素進(jìn)行映射. 分別去執(zhí)行 function

hash : 獲取到對(duì)象的哈希值(int, str, bool, tuple). hash算法:(1) 目的是唯一性 (2) dict 查找效率非常高, hash表.用空間換的時(shí)間 比較耗費(fèi)內(nèi)存

pytorch 常用函數(shù)參數(shù)詳解

1、torch.cat(inputs, dim=0) - Tensor?

參考鏈接:

[Pytorch] 詳解 torch.cat()

Pytorch學(xué)習(xí)筆記(一):torch.cat()模塊的詳解

函數(shù)作用:cat 是 concatnate 的意思:拼接,聯(lián)系在一起。在給定維度上對(duì)輸入的 Tensor 序列進(jìn)行拼接操作。torch.cat 可以看作是 torch.split 和 torch.chunk 的反操作

參數(shù):

inputs(sequence of Tensors):可以是任意相同類型的 Tensor 的 python 序列

dim(int, optional):defaults=0

dim=0: 按列進(jìn)行拼接?

dim=1: 按行進(jìn)行拼接

dim=-1: 如果行和列數(shù)都相同則按行進(jìn)行拼接,否則按照行數(shù)或列數(shù)相等的維度進(jìn)行拼接

假設(shè) a 和 b 都是 Tensor,且 a 的維度為 [2, 3],b 的維度為 [2, 4],則

torch.cat((a, b), dim=1) 的維度為 [2, 7]

2、torch.nn.CrossEntropyLoss()

函數(shù)作用:CrossEntropy 是交叉熵的意思,故而 CrossEntropyLoss 的作用是計(jì)算交叉熵。CrossEntropyLoss 函數(shù)是將 torch.nn.Softmax 和 torch.nn.NLLLoss 兩個(gè)函數(shù)組合在一起使用,故而傳入的預(yù)測(cè)值不需要先進(jìn)行 torch.nnSoftmax 操作。

參數(shù):

input(N, C):N 是 batch_size,C 則是類別數(shù),即在定義模型輸出時(shí),輸出節(jié)點(diǎn)個(gè)數(shù)要定義為 [N, C]。其中特別注意的是 target 的數(shù)據(jù)類型需要是浮點(diǎn)數(shù),即 float32

target(N):N 是 batch_size,故 target 需要是 1D 張量。其中特別注意的是 target 的數(shù)據(jù)類型需要是 long,即 int64

例子:

loss = nn.CrossEntropyLoss()

input = torch.randn(3, 5, requires_grad=True, dtype=torch.float32)

target = torch.empty(3, dtype=torch.long).random_(5)

output = loss(input, target)

output

輸出為:

tensor(1.6916, grad_fn=NllLossBackward)

本文名稱:python常用函數(shù)解釋的簡(jiǎn)單介紹
網(wǎng)站地址:http://muchs.cn/article0/hjdeoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、網(wǎng)頁設(shè)計(jì)公司、搜索引擎優(yōu)化關(guān)鍵詞優(yōu)化、網(wǎng)站改版、電子商務(wù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)