python3清除函數(shù) python如何清除

python3去除字符串(string)空格的五種方法

成年人的愛(ài)情不僅僅是簡(jiǎn)單的我愛(ài)你和漂亮的新衣服。

創(chuàng)新互聯(lián)建站主要從事成都做網(wǎng)站、網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)三沙,十載網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):18982081108

上一篇: python3將兩個(gè)列表合并成字典

下一篇: python3 map()函數(shù)

1、strip方法去掉字符串兩邊(開(kāi)頭和結(jié)尾)的空格

2、lstrip方法去掉字符串左邊的空格

3、rstrip方法去掉字符串右邊的空格

4、replace方法替換字符串的空格為空

注意: 這里說(shuō)一下replace方法的具體用法

old_str:原字符串需要替換的內(nèi)容,new_str:將old_str替換成的內(nèi)容,max:代表替換的次數(shù),默認(rèn)全部替換

5、正則匹配替換空格

正則方法的使用這里不多說(shuō)了,自己查一下詳細(xì)文檔即可。

如果感覺(jué)本文對(duì)您有幫助可以點(diǎn)個(gè)贊哦

本文僅供交流學(xué)習(xí),請(qǐng)勿用于非法途徑

僅是個(gè)人意見(jiàn),如有想法,歡迎留言

python數(shù)組中怎樣刪除符合條件的元素

使用filter來(lái)實(shí)現(xiàn),以python3為例,如果刪除列表中的所有0,則可使用下面代碼實(shí)現(xiàn):

a?=?[1,2,0,3,4,0,5,0,6]

b?=?filter(lambda?x:?x?!=?0,?a)

list(b)

效果如下:

注:如果使用python2則直接輸出b即可,在python3中filter返回結(jié)果為可迭代的對(duì)象,需使用list轉(zhuǎn)換成列表。

python字典操作函數(shù)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

printd

printd['two']

printd['three']

運(yùn)算結(jié)果:

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

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

1.2 通過(guò)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']

運(yùn)算結(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ò)關(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']

運(yùn)算結(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

運(yùn)算結(jié)果:

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

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

threeis3.

三、字典方法

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

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

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

printd

d.clear()

printd

運(yùn)算結(jié)果:

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

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

{}

請(qǐng)看下面兩個(gè)例子

3.1.1

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

d={}

dd=d

d['one']=1

d['two']=2

printdd

d={}

printd

printdd

運(yùn)算結(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

運(yùn)算結(jié)果:

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

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

{}

{}

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

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

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

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

printu'初始X字典:'

printx

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

y=x.copy()

printu'Y字典:'

printy

y['three']=33

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

printy

printx

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

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

printy

printx

運(yùn)算結(jié)果:

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

初始X字典:

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

X復(fù)制到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}

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

# _*_ 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

運(yùn)算輸出:

=======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ù):使用給定的鍵建立新的字典,鍵默認(rèn)對(duì)應(yīng)的值為None

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

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

printd

運(yùn)算輸出:

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

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

或者指定默認(rèn)的對(duì)應(yīng)值

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

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

printd

運(yùn)算結(jié)果:

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

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

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

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

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

printd

printd.get('one')

printd.get('four')

運(yùn)算結(jié)果:

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

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

1

None

注:get函數(shù)可以訪問(wèn)字典中不存在的鍵,當(dāng)該鍵不存在是返回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')

運(yùn)算結(jié)果:

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

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

True

False

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

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

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

printd

list=d.items()

forkey,valueinlist:

printkey,':',value

運(yùn)算結(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

運(yùn)算結(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

運(yùn)算結(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ù):刪除字典中對(duì)應(yīng)的鍵

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

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

printd

d.pop('one')

printd

運(yùn)算結(jié)果:

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

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

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

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

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

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

printd

d.popitem()

printd

運(yùn)算結(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

運(yùn)算結(jié)果:

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

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

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

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

d={

'one':123,

'two':2,

'three':3

}

printd

x={'one':1}

d.update(x)

printd

運(yùn)算結(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返回值得迭代器,由于在字典中值不是唯一的,所以列表中可以包含重復(fù)的元素

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

d={

'one':123,

'two':2,

'three':3,

'test':2

}

printd.values()

運(yùn)算結(jié)果:

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

[2,3,2,123]

python3--內(nèi)置函數(shù)

python的常用內(nèi)置函數(shù)

1.abs() 函數(shù)返回?cái)?shù)字的絕對(duì)值

abs(-40)=40

2. dict() 函數(shù)用于創(chuàng)建一個(gè)字典

dict()

{} ? ? ?#創(chuàng)建一個(gè)空字典類似于u={},字典的存取方式一般為key-value

例如u = {"username":"tom", ?"age":18}

3. help() 函數(shù)用于查看函數(shù)或模塊用途的詳細(xì)說(shuō)明

help('math')查看math模塊的用處

a=[1,2,3,4]

help(a)查看列表list幫助信息

4.dir()獲得當(dāng)前模塊的屬性列表

dir(help)

['__call__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

5.min() 方法返回給定參數(shù)的最小值 /參數(shù)可以為序列

a=? min(10,20,30,40)

a

10

6. next() 返回迭代器的下一個(gè)項(xiàng)目

it = iter([1, 2, 3, 4, 5])

next(it)

1

next(it)

2

7. id() 函數(shù)用于獲取對(duì)象的內(nèi)存地址

a=12

id(a)

1550569552

8.enumerate() 函數(shù)用于將一個(gè)可遍歷的數(shù)據(jù)對(duì)象(如列表、元組或字符串)組合為一個(gè)索引序列,同時(shí)列出數(shù)據(jù)和數(shù)據(jù)下標(biāo),一般用在 for 循環(huán)當(dāng)中。

a=["tom","marry","leblan"]

list(enumerate(a))

[(0, 'tom'), (1, 'marry'), (2, 'leblan')]

9. oct() 函數(shù)將一個(gè)整數(shù)轉(zhuǎn)換成8進(jìn)制字符串

oct(15)

'0o17'

oct(10)

'0o12'

10. bin() 返回一個(gè)整數(shù) int 或者長(zhǎng)整數(shù) long int 的二進(jìn)制表示

bin(10)

'0b1010'

bin(15)

'0b1111'

11.eval() 函數(shù)用來(lái)執(zhí)行一個(gè)字符串表達(dá)式,并返回表達(dá)式的值

eval('2+2')

4

12.int() 函數(shù)用于將一個(gè)字符串會(huì)數(shù)字轉(zhuǎn)換為整型

int(3)

3

int(3.6)

3

int(3.9)

3

int(4.0)

4

13.open() 函數(shù)用于打開(kāi)一個(gè)文件,創(chuàng)建一個(gè)file對(duì)象,相關(guān)的方法才可以調(diào)用它進(jìn)行讀寫

f=open('test.txt')

14.str() 函數(shù)將對(duì)象轉(zhuǎn)化為適于人閱讀的形式

str(3)

'3'

15. bool() 函數(shù)用于將給定參數(shù)轉(zhuǎn)換為布爾類型,如果沒(méi)有參數(shù),返回 False

bool()

False

bool(1)

True

bool(10)

True

bool(10.0)

True

16.isinstance() 函數(shù)來(lái)判斷一個(gè)對(duì)象是否是一個(gè)已知的類型

a=5

isinstance(a,int)

True

isinstance(a,str)

False

17. sum() 方法對(duì)系列進(jìn)行求和計(jì)算

sum([1,2,3],5)

11

sum([1,2,3])

6

18. super() 函數(shù)用于調(diào)用下一個(gè)父類(超類)并返回該父類實(shí)例的方法。super 是用來(lái)解決多重繼承問(wèn)題的,直接用類名調(diào)用父類方法

class ? User(object):

? def__init__(self):

class Persons(User):

? ? ? ? super(Persons,self).__init__()

19. float() 函數(shù)用于將整數(shù)和字符串轉(zhuǎn)換成浮點(diǎn)數(shù)

float(1)

1.0

float(10)

10.0

20. iter() 函數(shù)用來(lái)生成迭代器

a=[1,2,3,4,5,6]

iter(a)

for i in iter(a):

... ? ? ? ? print(i)

...

1

2

3

4

5

6

21.tuple 函數(shù)將列表轉(zhuǎn)換為元組

a=[1,2,3,4,5,6]

tuple(a)

(1, 2, 3, 4, 5, 6)

22.len() 方法返回對(duì)象(字符、列表、元組等)長(zhǎng)度或項(xiàng)目個(gè)數(shù)

s = "playbasketball"

len(s)

14

a=[1,2,3,4,5,6]

len(a)

6

23. property() 函數(shù)的作用是在新式類中返回屬性值

class User(object):

?def __init__(self,name):

? ? ? ? ? self.name = name

def get_name(self):

? ? ? ? ? return self.get_name

@property

?def name(self):

? ? ? ? ?return self_name

24.type() 函數(shù)返回對(duì)象的類型

25.list() 方法用于將元組轉(zhuǎn)換為列表

b=(1,2,3,4,5,6)

list(b)

[1, 2, 3, 4, 5, 6]

26.range() 函數(shù)可創(chuàng)建一個(gè)整數(shù)列表,一般用在 for 循環(huán)中

range(10)

range(0, 10)

range(10,20)

range(10, 20)

27. getattr() 函數(shù)用于返回一個(gè)對(duì)象屬性值

class w(object):

... ? ? ? ? ? ? s=5

...

a = w()

getattr(a,'s')

5

28. complex() 函數(shù)用于創(chuàng)建一個(gè)復(fù)數(shù)或者轉(zhuǎn)化一個(gè)字符串或數(shù)為復(fù)數(shù)。如果第一個(gè)參數(shù)為字符串,則不需要指定第二個(gè)參數(shù)

complex(1,2)

(1+2j)

complex(1)

(1+0j)

complex("1")

(1+0j)

29.max() 方法返回給定參數(shù)的最大值,參數(shù)可以為序列

b=(1,2,3,4,5,6)

max(b)

6

30. round() 方法返回浮點(diǎn)數(shù)x的四舍五入值

round(10.56)

11

round(10.45)

10

round(10.45,1)

10.4

round(10.56,1)

10.6

round(10.565,2)

10.56

31. delattr 函數(shù)用于刪除屬性

class Num(object):

...? ? a=1

...? ? b=2

...? ? c=3.

.. print1 = Num()

print('a=',print1.a)

a= 1

print('b=',print1.b)

b= 2

print('c=',print1.c)

c= 3

delattr(Num,'b')

print('b=',print1.b)

Traceback (most recent call last):? File "", line 1, inAttributeError: 'Num' object has no attribute 'b'

32. hash() 用于獲取取一個(gè)對(duì)象(字符串或者數(shù)值等)的哈希值

hash(2)

2

hash("tom")

-1675102375494872622

33. set() 函數(shù)創(chuàng)建一個(gè)無(wú)序不重復(fù)元素集,可進(jìn)行關(guān)系測(cè)試,刪除重復(fù)數(shù)據(jù),還可以計(jì)算交集、差集、并集等。

a= set("tom")

b = set("marrt")

a,b

({'t', 'm', 'o'}, {'m', 't', 'a', 'r'})

ab#交集

{'t', 'm'}

a|b#并集

{'t', 'm', 'r', 'o', 'a'}

a-b#差集

{'o'}

文章標(biāo)題:python3清除函數(shù) python如何清除
標(biāo)題來(lái)源:http://www.muchs.cn/article8/doeogop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)微信小程序、網(wǎng)站內(nèi)鏈、全網(wǎng)營(yíng)銷推廣、用戶體驗(yàn)、標(biāo)簽優(yōu)化

廣告

聲明:本網(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)