python字符操作函數(shù),python常用字符串函數(shù)

python字典操作函數(shù)

字典是一種通過名字或者關(guān)鍵字引用的得數(shù)據(jù)結(jié)構(gòu),其鍵可以是數(shù)字、字符串、元組,這種結(jié)構(gòu)類型也稱之為映射。字典類型是Python中唯一內(nèi)建的映射類型,基本的操作包括如下:

創(chuàng)新互聯(lián)主要從事網(wǎng)站制作、成都網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)迎澤,10多年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792

(1)len():返回字典中鍵—值對的數(shù)量;

(2)d[k]:返回關(guān)鍵字對于的值;

(3)d[k]=v:將值關(guān)聯(lián)到鍵值k上;

(4)del d[k]:刪除鍵值為k的項;

(5)key in d:鍵值key是否在d中,是返回True,否則返回False。

(6)clear函數(shù):清除字典中的所有項

(7)copy函數(shù):返回一個具有相同鍵值的新字典;deepcopy()函數(shù)使用深復制,復制其包含所有的值,這個方法可以解決由于副本修改而使原始字典也變化的問題

(8)fromkeys函數(shù):使用給定的鍵建立新的字典,鍵默認對應(yīng)的值為None

(9)get函數(shù):訪問字典成員

(10)has_key函數(shù):檢查字典中是否含有給出的鍵

(11)items和iteritems函數(shù):items將所有的字典項以列表方式返回,列表中項來自(鍵,值),iteritems與items作用相似,但是返回的是一個迭代器對象而不是列表

(12)keys和iterkeys:keys將字典中的鍵以列表形式返回,iterkeys返回鍵的迭代器

(13)pop函數(shù):刪除字典中對應(yīng)的鍵

(14)popitem函數(shù):移出字典中的項

(15)setdefault函數(shù):類似于get方法,獲取與給定鍵相關(guān)聯(lián)的值,也可以在字典中不包含給定鍵的情況下設(shè)定相應(yīng)的鍵值

(16)update函數(shù):用一個字典更新另外一個字典

(17)?values和itervalues函數(shù):values以列表的形式返回字典中的值,itervalues返回值得迭代器,由于在字典中值不是唯一的,所以列表中可以包含重復的元素

一、字典的創(chuàng)建

1.1 直接創(chuàng)建字典

d={'one':1,'two':2,'three':3}

printd

printd['two']

printd['three']

運算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

1.2 通過dict創(chuàng)建字典

# _*_ coding:utf-8 _*_

items=[('one',1),('two',2),('three',3),('four',4)]

printu'items中的內(nèi)容:'

printitems

printu'利用dict創(chuàng)建字典,輸出字典內(nèi)容:'

d=dict(items)

printd

printu'查詢字典中的內(nèi)容:'

printd['one']

printd['three']

運算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

items中的內(nèi)容:

[('one',1), ('two',2), ('three',3), ('four',4)]

利用dict創(chuàng)建字典,輸出字典內(nèi)容:

{'four':4,'three':3,'two':2,'one':1}

查詢字典中的內(nèi)容:

或者通過關(guān)鍵字創(chuàng)建字典

# _*_ coding:utf-8 _*_

d=dict(one=1,two=2,three=3)

printu'輸出字典內(nèi)容:'

printd

printu'查詢字典中的內(nèi)容:'

printd['one']

printd['three']

運算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

輸出字典內(nèi)容:

{'three':3,'two':2,'one':1}

查詢字典中的內(nèi)容:

二、字典的格式化字符串

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3,'four':4}

printd

print"three is %(three)s."%d

運算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'four':4,'three':3,'two':2,'one':1}

threeis3.

三、字典方法

3.1?clear函數(shù):清除字典中的所有項

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3,'four':4}

printd

d.clear()

printd

運算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'four':4,'three':3,'two':2,'one':1}

{}

請看下面兩個例子

3.1.1

# _*_ coding:utf-8 _*_

d={}

dd=d

d['one']=1

d['two']=2

printdd

d={}

printd

printdd

運算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'two':2,'one':1}

{}

{'two':2,'one':1}

3.1.2

# _*_ coding:utf-8 _*_

d={}

dd=d

d['one']=1

d['two']=2

printdd

d.clear()

printd

printdd

運算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'two':2,'one':1}

{}

{}

3.1.2與3.1.1唯一不同的是在對字典d的清空處理上,3.1.1將d關(guān)聯(lián)到一個新的空字典上,這種方式對字典dd是沒有影響的,所以在字典d被置空后,字典dd里面的值仍舊沒有變化。但是在3.1.2中clear方法清空字典d中的內(nèi)容,clear是一個原地操作的方法,使得d中的內(nèi)容全部被置空,這樣dd所指向的空間也被置空。

3.2?copy函數(shù):返回一個具有相同鍵值的新字典

# _*_ coding:utf-8 _*_

x={'one':1,'two':2,'three':3,'test':['a','b','c']}

printu'初始X字典:'

printx

printu'X復制到Y(jié):'

y=x.copy()

printu'Y字典:'

printy

y['three']=33

printu'修改Y中的值,觀察輸出:'

printy

printx

printu'刪除Y中的值,觀察輸出'

y['test'].remove('c')

printy

printx

運算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

初始X字典:

{'test': ['a','b','c'],'three':3,'two':2,'one':1}

X復制到Y(jié):

Y字典:

{'test': ['a','b','c'],'one':1,'three':3,'two':2}

修改Y中的值,觀察輸出:

{'test': ['a','b','c'],'one':1,'three':33,'two':2}

{'test': ['a','b','c'],'three':3,'two':2,'one':1}

刪除Y中的值,觀察輸出

{'test': ['a','b'],'one':1,'three':33,'two':2}

{'test': ['a','b'],'three':3,'two':2,'one':1}

注:在復制的副本中對值進行替換后,對原來的字典不產(chǎn)生影響,但是如果修改了副本,原始的字典也會被修改。deepcopy函數(shù)使用深復制,復制其包含所有的值,這個方法可以解決由于副本修改而使原始字典也變化的問題。

# _*_ coding:utf-8 _*_

fromcopyimportdeepcopy

x={}

x['test']=['a','b','c','d']

y=x.copy()

z=deepcopy(x)

printu'輸出:'

printy

printz

printu'修改后輸出:'

x['test'].append('e')

printy

printz

運算輸出:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

輸出:

{'test': ['a','b','c','d']}

{'test': ['a','b','c','d']}

修改后輸出:

{'test': ['a','b','c','d','e']}

{'test': ['a','b','c','d']}

3.3?fromkeys函數(shù):使用給定的鍵建立新的字典,鍵默認對應(yīng)的值為None

# _*_ coding:utf-8 _*_

d=dict.fromkeys(['one','two','three'])

printd

運算輸出:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':None,'two':None,'one':None}

或者指定默認的對應(yīng)值

# _*_ coding:utf-8 _*_

d=dict.fromkeys(['one','two','three'],'unknow')

printd

運算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':'unknow','two':'unknow','one':'unknow'}

3.4?get函數(shù):訪問字典成員

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

printd.get('one')

printd.get('four')

運算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

1

None

注:get函數(shù)可以訪問字典中不存在的鍵,當該鍵不存在是返回None

3.5?has_key函數(shù):檢查字典中是否含有給出的鍵

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

printd.has_key('one')

printd.has_key('four')

運算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

True

False

3.6?items和iteritems函數(shù):items將所有的字典項以列表方式返回,列表中項來自(鍵,值),iteritems與items作用相似,但是返回的是一個迭代器對象而不是列表

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

list=d.items()

forkey,valueinlist:

printkey,':',value

運算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

three :3

two :2

one :1

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

it=d.iteritems()

fork,vinit:

print"d[%s]="%k,v

運算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

d[three]=3

d[two]=2

d[one]=1

3.7?keys和iterkeys:keys將字典中的鍵以列表形式返回,iterkeys返回鍵的迭代器

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

printu'keys方法:'

list=d.keys()

printlist

printu'\niterkeys方法:'

it=d.iterkeys()

forxinit:

printx

運算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

keys方法:

['three','two','one']

iterkeys方法:

three

two

one

3.8?pop函數(shù):刪除字典中對應(yīng)的鍵

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

d.pop('one')

printd

運算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

{'three':3,'two':2}

3.9?popitem函數(shù):移出字典中的項

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

d.popitem()

printd

運算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

{'two':2,'one':1}

3.10?setdefault函數(shù):類似于get方法,獲取與給定鍵相關(guān)聯(lián)的值,也可以在字典中不包含給定鍵的情況下設(shè)定相應(yīng)的鍵值

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

printd.setdefault('one',1)

printd.setdefault('four',4)

printd

運算結(jié)果:

{'three':3,'two':2,'one':1}

{'four':4,'three':3,'two':2,'one':1}

3.11?update函數(shù):用一個字典更新另外一個字典

# _*_ coding:utf-8 _*_

d={

'one':123,

'two':2,

'three':3

}

printd

x={'one':1}

d.update(x)

printd

運算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':123}

{'three':3,'two':2,'one':1}

3.12?values和itervalues函數(shù):values以列表的形式返回字典中的值,itervalues返回值得迭代器,由于在字典中值不是唯一的,所以列表中可以包含重復的元素

# _*_ coding:utf-8 _*_

d={

'one':123,

'two':2,

'three':3,

'test':2

}

printd.values()

運算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

[2,3,2,123]

len函數(shù)python用法

python中l(wèi)en()的用法:

新建一個len()函數(shù)的使用py,中文編碼聲明注釋:#coding=gbk,函數(shù):len()作用:返回字符串、列表、字典、元組等長度。語法:len(str)。參數(shù):str:要計算的字符串、列表、字典、元組等。返回值:字符串、列表、字典、元組等元素的長度。

實例:

1、計算字符串的長度。

2、計算列表的元素個數(shù)。

3、計算字典的總長度,即鍵值對總數(shù)。

4、計算元組元素個數(shù)。

在Python中,要知道一個字符串有多少個字符,以獲得字符串的長度,或者一個字符串需要多少字節(jié),可以使用len函數(shù)。

Python3之字符串格式化format函數(shù)詳解(上)

概述

在Python3中,字符串格式化操作通過format()方法或者f'string'實現(xiàn)。而相比于老版的字符串格式化方式,format()方法擁有更多的功能,操作起來更加方便,可讀性也更強。該函數(shù)將字符串當成一個模板,通過傳入的參數(shù)進行格式化,并且使用大括號{}作為特殊字符代替%。

位置設(shè)定

默認位置

不指定格式化位置,按照默認順序格式化

示例結(jié)果:

設(shè)置位置

設(shè)置數(shù)字順序指定格式化的位置

示例結(jié)果:

設(shè)置關(guān)鍵字

設(shè)置關(guān)鍵字指定格式化的內(nèi)容

示例結(jié)果:

參數(shù)傳遞

我們可以傳入各種類型參數(shù)格式化字符串,即不限于字符串變量或數(shù)字等。

元組傳參

利用元組傳參,傳參形式 *tuple

示例結(jié)果:

字典傳參

示例結(jié)果:

列表傳參

示例結(jié)果:

python count()函數(shù)的功能和用法

python count()函數(shù)的功能和用法如下:

統(tǒng)計字符串

在python中可以使用“count()”函數(shù)統(tǒng)計字符串里某個字符出現(xiàn)的次數(shù),該函數(shù)用于統(tǒng)計次數(shù),其語法是“count(sub, start...

Python count() 方法用于統(tǒng)計字符串里某個字符出現(xiàn)的次數(shù)??蛇x參數(shù)為在字符串搜索的開始與結(jié)束位置。

count()函數(shù)

描述:統(tǒng)計字符串里某個字符出現(xiàn)的次數(shù)。可以選擇字符串索引的起始位置和結(jié)束位置。? ? ? ? ?

語法:str.count("char", start,end)? 或 str.count("char")? ? - int? ? 返回整數(shù)

str —— 為要統(tǒng)計的字符(可以是單字符,也可以是多字符)。

star —— 為索引字符串的起始位置,默認參數(shù)為0。

end —— 為索引字符串的結(jié)束位置,默認參數(shù)為字符串長度即len(str)

分享題目:python字符操作函數(shù),python常用字符串函數(shù)
當前地址:http://muchs.cn/article26/hcjscg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、軟件開發(fā)、網(wǎng)站維護、定制網(wǎng)站域名注冊

廣告

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

微信小程序開發(fā)