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