36 字典中的items方法和key pop方法popitem方法setdefault update

第十課 獲取字典中的key和value(items方法和key方法)
# items方法和keys方法 
# items 方法用於返回字典中全部的key  value 對  得到的每個kv對用一個元組來表示  items方法返回的值被成爲字典視圖的一個特殊類型 能夠直接用於迭代 for 循環裏面 
# items 的返回值 與字典使用的一樣的值  咱們修改了一方的值,另一方也會一樣改變。
'''
key 方法用於返回字典中全部的key 與items方法相似,也能夠用於迭代 for循環 
'''

d = {'help':'幫助','bike':'自行車','plane':'飛機','China':'中國'}
print(d.items())                # dict_items([('help', '幫助'), ('bike', '自行車'), ('plane', '飛機'), ('China', '中國')]) 輸出的每個元素就是一個元組 

for key_value in d.items():
    print("key","=",key_value[0],"value","=",key_value[1])   
'''
key = help value = 幫助
key = bike value = 自行車
key = plane value = 飛機
key = China value = 中國
'''

for key,value in d.items():
    print("{} = {}".format(key,value))
"""
help = 幫助
bike = 自行車
plane = 飛機
China = 中國
"""

print(('bike','自行車') in d.items())   # True 

dict_items = d.items()
d['bike'] = '自行車;摩托車;電動自行車'
print(dict_items)                         # dict_items([('help', '幫助'), ('bike', '自行車;摩托車;電動自行車'), ('plane', '飛機'), ('China', '中國')])

print(d.keys())                             # dict_keys(['help', 'bike', 'plane', 'China'])

for key in d.keys():
    print(key)                          

'''
help
bike
plane
China
'''

----------------------------------------------------
第十一課 彈出字典中的值(pop方法和popitem方法)

# pop方法和popitem方法

# pop方法,用於獲取指定的key的值,並從字典中彈出這個key-value對
# popitem方法用於返回字典中最後一個key-value對,並彈出key-value對

d = {'c':10,'a':40,'b':12,'x':45}
d['1'] = 20    # 首先先添加2組 元素
d['5'] = 100
print(d)      # {'c': 10, 'a': 40, 'b': 12, 'x': 45, '1': 20, '5': 100}

print(d.pop('a'))    # 40 獲取指定key的值 
print(d)             #{'c': 10, 'b': 12, 'x': 45, '1': 20, '5': 100}      a的這個值的kv元素一塊兒彈出來了 
#40
#{'c': 10, 'b': 12, 'x': 45, '1': 20, '5': 100}

print(d.popitem())    # ('5', 100) 

for i in range(len(d)):
    print(d.popitem())

'''
輸出結果爲:
('5', 100)
('1', 20)
('x', 45)
('b', 12)
('c', 10)   
'''

----------------------------------------------------
第十二課 添加但不修改value(setdefault方法)
# setdefault方法: 若是這個key在字典中不存在,那麼就在字典中添加一組值,而後返回value;若是這個key在字典中已經存在了,那麼就會忽略後面的值,返回在字典中存在的值。若是咱們不指定第二個參數值,那麼就會返回None 

d = {"name":"Bill", "age":30}
d['salary'] = 2000
d['age'] = 50          # 如何這個元素在字典中不存在,那麼在後面會添加一組,若是存在那麼久修改他的value

print(d) #{'name': 'Bill', 'age': 50, 'salary': 2000}

# None
print(d.setdefault("location","中國"))
print(d)
'''
中國
{'name': 'Bill', 'age': 50, 'salary': 2000, 'location': '中國'}
'''
print(d.setdefault("location", "德國"))
print(d)
'''
中國
{'name': 'Bill', 'age': 50, 'salary': 2000, 'location': '中國'}
'''

print(d.setdefault("abc"))
print(d)
'''
None
{'name': 'Bill', 'age': 50, 'salary': 2000, 'location': '中國', 'abc': None}
'''

'''
中國
{'name': 'Bill', 'age': 50, 'salary': 2000, 'location': '中國'}
中國
{'name': 'Bill', 'age': 50, 'salary': 2000, 'location': '中國'}
None
{'name': 'Bill', 'age': 50, 'salary': 2000, 'location': '中國', 'abc': None}
'''
----------------------------------------
第十三課 成批導入字典數據(update方法)
# update方法
# 用一個字典中的key-value對更新另一個字典,該方法接收一個參數
# 用做數據源的字典
# dict1.update(dict2)   沒有就插入,有的話,就更新

d1 = {
    'title':'歐瑞學院',
    'website':'https://geekori.com',
    'description':'從事在線IT課程研發和銷售'    
}

d2 = {
    'title':'歐瑞科技',
    'products':['歐瑞學院','博客','讀書頻道','極客題庫','OriUnity'],
    'description':'從事在線IT課程研發和銷售,工具軟件研發'
    }
d1.update(d2)    # 用d2的字典去改變 d1  沒有就直接增長,有的話,就用d2中的字典的值去更新update
print(d1)
'''
{'title': '歐瑞科技', 'website': 'https://geekori.com', 'description': '從事在線IT課程研發和銷售,工具軟件研發', 'products': ['歐瑞學院', '博客', '讀書頻道', '極客題庫', 'OriUnity']}
'''

------------------------------------------------
第十四課 獲取字典中值的列表(values方法)
# values方法: 獲取字典中值的列表,value是能夠重複的,能夠有多個同樣的

# keys方法: 用於返回字典中key的列表  key是惟一的,不能夠重複的。
# items方法: 想獲得字典中全部的key 和 value ; 要結合for 循環 

d = {
    'a':1,
    'b':2,
    'c':2,
    'd':3,
    'e':4,
    'e':40
}
print(d) # {'a': 1, 'b': 2, 'c': 2, 'd': 3, 'e': 40}     這裏面e 的值被後面的覆蓋了 
print(d.values()) # dict_values([1, 2, 2, 3, 40])
print(d.keys()) # dict_keys(['a', 'b', 'c', 'd', 'e'])

for value in d.values():
    print(value)
    '''
1
2
2
3
40
    '''
# keys(只獲取key)、values(只獲取value)、items(獲取key 和value)
相關文章
相關標籤/搜索