Python字典練習
阿新 • • 發佈:2017-07-31
彈出 函數 remove python3 mov text 4.3 san 說明符
#字典:通過名字引用值得數據結構 鍵/值
from copy import deepcopy
#######################################################
"""
#1 創建和使用字典
phone = {"張三":"1234567","lisi":"23414","lisi":"23414","wangwu":"432653250"}
print(phone)
print(phone["張三"])#鍵是唯一的
print(phone["lisi"])
#dict函數創建字典
item = [(‘name‘,"zhangsan"),(‘age‘,42)]
d = dict(item)
print(d)
"""
"""
C:\python3.7\python.exe D:/Python-Test/qiubai/qiubai/Test3.py
{‘張三‘: ‘1234567‘, ‘lisi‘: ‘23414‘, ‘wangwu‘: ‘432653250‘}
1234567
23414
{‘name‘: ‘zhangsan‘, ‘age‘: 42}
"""
#######################################################
"""
#2 字典示例
people = {
‘zhangsan1‘:{
‘phone‘:12345,
‘addr‘:"beijing"
},
‘zhangsan2‘:{
‘phone‘:12345,
‘addr‘:"beijing"
},
‘zhangsan3‘:{
‘phone‘:12345,
‘addr‘:"beijing"
},
‘zhangsan‘:{
‘phone‘:12345,
‘addr‘:"beijing"
}
}
labers = {
‘phone‘:‘電話號碼‘,
‘addr‘:‘地址‘
}
name = input(‘請輸入姓名:‘)
request = input(‘地址a還是電話p?‘)
if request == ‘p‘:
key =‘phone‘
if request == ‘a‘:
key = ‘addr‘
if name in people:
print("%s‘s %s is %s" % (name, labers[key],people[name][key]))
"""
"""
C:\python3.7\python.exe D:/Python-Test/qiubai/qiubai/Test3.py
請輸入姓名:zhangsan
地址a還是電話p?p
zhangsan‘s 電話號碼 is 12345
"""
#######################################################
‘‘‘
#3 字符串格式化
#轉化說明符%(key)s --> % data
tmp = """
<head>
<title>%(title)s<title/>
</head>
<body>
<h1>%(title)s</h1>
<p>%(text)s</p>
</body>
"""
data = {
‘title‘:"My Home Page",
‘text‘:"Welcome to my page"
}
print(tmp % data)
‘‘‘
"""
C:\python3.7\python.exe D:/Python-Test/qiubai/qiubai/Test3.py
<head>
<title>My Home Page<title/>
</head>
<body>
<h1>My Home Page</h1>
<p>Welcome to my page</p>
</body>
"""
#######################################################
"""
#4 字典方法
#4.1 clear清除字典所有項
data1 = {
}
data1[‘name‘] = "zhangsan"
data1[‘age‘] = 45
print(data1)
data1.clear()
print(data1)
"""
"""
C:\python3.7\python.exe D:/Python-Test/qiubai/qiubai/Test3.py
{‘name‘: ‘zhangsan‘, ‘age‘: 45}
{}
"""
#######################################################
‘‘‘
#4.2 copy淺復制
x = {
‘name‘:‘zhangsan‘,
‘love‘:[‘reading‘,‘play‘,‘sing‘]
}
y = x.copy()
print(x)
print("復制一份:",y)
#替換
y[‘name‘] = ‘lishi‘
#修改
y[‘love‘].remove(‘play‘)
print("替換後原值不受影響,修改副本後原值會受到影響:",x)
print(y)
‘‘‘
"""
C:\python3.7\python.exe D:/Python-Test/qiubai/qiubai/Test3.py
{‘name‘: ‘zhangsan‘, ‘love‘: [‘reading‘, ‘play‘, ‘sing‘]}
復制一份: {‘name‘: ‘zhangsan‘, ‘love‘: [‘reading‘, ‘play‘, ‘sing‘]}
替換後原值不受影響,修改副本後原值會受到影響: {‘name‘: ‘zhangsan‘, ‘love‘: [‘reading‘, ‘sing‘]}
{‘name‘: ‘lishi‘, ‘love‘: [‘reading‘, ‘sing‘]}
"""
#######################################################
"""
#4.3 深copy
d = {}
d[‘name‘] = [‘zhang‘,‘zhao‘,‘li‘]
c = d.copy()#淺copy
dc = deepcopy(d)#深copy 更改值後不受影響
d[‘name‘].append(‘wang‘)#添加元素
print(c)
print(dc)
"""
"""
C:\python3.7\python.exe D:/Python-Test/qiubai/qiubai/Test3.py
{‘name‘: [‘zhang‘, ‘zhao‘, ‘li‘, ‘wang‘]}
{‘name‘: [‘zhang‘, ‘zhao‘, ‘li‘]}
"""
#######################################################
"""
#5 fromkeys:使用給定的鍵建立新的字典
print({}.fromkeys([‘name‘,‘age‘,‘afd‘]))
print({}.fromkeys([‘name‘,‘age‘,‘afd‘],‘默認‘))
"""
"""
C:\python3.7\python.exe D:/Python-Test/qiubai/qiubai/Test3.py
{‘name‘: None, ‘age‘: None, ‘afd‘: None}
{‘name‘: ‘默認‘, ‘age‘: ‘默認‘, ‘afd‘: ‘默認‘}
"""
#######################################################
‘‘‘
# get訪問不存在的鍵不會報錯
d = {‘name‘:‘zhangsan‘,‘age‘:45,‘adr‘:"beijing"}
print(d.get(‘adr‘))
print(d.get(‘price‘))
print(d.get(‘adr3‘,‘shanghai‘))#如果不存在 更改NONE為shanghai
‘‘‘
"""
C:\python3.7\python.exe D:/Python-Test/qiubai/qiubai/Test3.py
beijing
None
shanghai
"""
#######################################################
‘‘‘
#6 items 將所有字典以列表方式返回 pop方法獲取給定鍵值 然後將其從字典裏刪除 popitem彈出列表最後一個元素(隨機)
d = {‘name‘:‘zhangsan‘,‘age‘:45,‘adr‘:"beijing"}
print(d.items())
print(d.pop(‘name‘),"d:",d)
print(d.popitem())
print(d.popitem())
print(d)
‘‘‘
"""
C:\python3.7\python.exe D:/Python-Test/qiubai/qiubai/Test3.py
dict_items([(‘name‘, ‘zhangsan‘), (‘age‘, 45), (‘adr‘, ‘beijing‘)])zhangsan d: {‘age‘: 45, ‘adr‘: ‘beijing‘}(‘adr‘, ‘beijing‘)(‘age‘, 45){}"""
Python字典練習