IT培訓(xùn)網(wǎng)
IT在線學(xué)習(xí)
字典的popitem()方法和列表的pop()方法很像,都是“彈出”并返回一個(gè)元素。在Python 3.5及之前的版本中,字典是無序的,因此,popitem()是隨機(jī)彈出一個(gè)項(xiàng);但從Python 3.6開始,字典是按插入的順序排列的,因此,popitem()彈出的是最后一個(gè)項(xiàng)。
首先驗(yàn)證一下Python 3.5和3.6這兩個(gè)版本的有序性(Python 2和這兩個(gè)版本的表現(xiàn)也均不一致,本書不詳細(xì)探討)。在Python 3.5中:
- >>> someone = {'name': 'Wang', 'age': 19, 'hobbies': ['sing songs', 'dance', 'basketball']}
- >>> someone # 查看someone
- {'name': 'Wang', 'age': 19, 'hobbies': ['sing songs', 'dance', 'basketball']}
- >>> someone # 再次查看someone,發(fā)現(xiàn)它并沒有改變
- {'name': 'Wang', 'age': 19, 'hobbies': ['sing songs', 'dance', 'basketball']}
然后關(guān)閉Python交互模式,再重新進(jìn)入Python交互模式:
- >>> someone = {'name': 'Wang', 'age': 19, 'hobbies': ['sing songs', 'dance', 'basketball']}
- >>> someone # someone字典鍵-值對(duì)的順序發(fā)生了改變
- {'hobbies': ['sing songs', 'dance', 'basketball'], 'name': 'Wang', 'age': 19}
- >>> someone
- {'hobbies': ['sing songs', 'dance', 'basketball'], 'name': 'Wang', 'age': 19}
由于在Python 3.5及之前的版本中,字典是無序的,因此,上面的情況可能會(huì)出現(xiàn)(由于是隨機(jī)的,當(dāng)然也可能順序不變)。但是從Python 3.6開始,執(zhí)行和前面一樣的操作,無論做多少次,都會(huì)得到下面的結(jié)果:
- >>> someone = {'name': 'Wang', 'age': 19, 'hobbies': ['sing songs', 'dance', 'basketball']}
- >>> someone
- {'name': 'Wang', 'age': 19, 'hobbies': ['sing songs', 'dance', 'basketball']}
- >>> someone
- {'name': 'Wang', 'age': 19, 'hobbies': ['sing songs', 'dance', 'basketball']}
確認(rèn)Python 3.6中字典是有序的之后,嘗試使用popitems()方法:
- >>> someone = {'name': 'Wang', 'age': 19, 'hobbies': ['sing songs', 'dance', 'basketball']}
- >>> someone.popitem() # 彈出并返回最后一個(gè)項(xiàng)
- ('hobbies', ['sing songs', 'dance', 'basketball'])
- >>> someone.popitem()
- ('age', 19)
- >>> someone.popitem()
- ('name', 'Wang')
- >>> someone.popitem() # 如果字典為空,Python解釋器會(huì)報(bào)錯(cuò)
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- KeyError: 'popitem(): dictionary is empty'
5、copy()方法
與列表的copy()方法類似,字典的copy()方法返回一個(gè)和被拷貝字典相同的字典(同樣是淺復(fù)制):
- >>> someone = {'name': 'Wang', 'age': 19, 'hobbies': ['sing songs', 'dance', 'basketball']}
- >>> other = someone.copy()
- >>> other
- {'name': 'Wang', 'age': 19, 'hobbies': ['sing songs', 'dance', 'basketball']}
- >>> other['gender'] = 'female' # other字典中添加一個(gè)鍵-值對(duì)
- >>> other # other字典改變了
- {'name': 'Wang', 'age': 19, 'hobbies': ['sing songs', 'dance', 'basketball'], 'gender': 'female'}
- >>> someone # someone字典并沒有改變
- {'name': 'Wang', 'age': 19, 'hobbies': ['sing songs', 'dance', 'basketball']}
6、fromkeys()方法
字典的fromkeys()方法用于根據(jù)指定序列創(chuàng)建一個(gè)新字典,該字典具有用戶提供的值。fromkeys()方法返回一個(gè)新字典,其中指定序列中的項(xiàng)作為字典的鍵。如果設(shè)置了value參數(shù),那么字典所有鍵對(duì)應(yīng)的值都將設(shè)置為提供的值;如果未設(shè)置,那么默認(rèn)值為None:
- >>> someone = {}.fromkeys(['name', 'age', 'hobbies']) # 默認(rèn)值為None
- >>> someone
- {'name': None, 'age': None, 'hobbies': None}
- >>> someone = {}.fromkeys(['name', 'age', 'hobbies'], 'a') # 指定默認(rèn)值
- >>> someone
- {'name': 'a', 'age': 'a', 'hobbies': 'a'}
7、clear()方法
字典的clear()方法用于清除字典中的所有項(xiàng):
- >>> someone = {'name': 'Wang', 'age': 19, 'hobbies': ['sing songs', 'dance', 'basketball']}
- >>> someone.clear()
- >>> someone # 此時(shí)someone字典是一個(gè)空字典
- {}
>>本文地址:http://hqfphsz.com/zhuanye/2020/51892.html
聲明:本站稿件版權(quán)均屬中公教育優(yōu)就業(yè)所有,未經(jīng)許可不得擅自轉(zhuǎn)載。
1 您的年齡
2 您的學(xué)歷
3 您更想做哪個(gè)方向的工作?