码迷,mamicode.com
首页 > 编程语言 > 详细

Python字典练习

时间:2017-07-31 16:25:51      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:int   数据   说明符   epc   tmp   引用   welcome   shang   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字典练习

标签:int   数据   说明符   epc   tmp   引用   welcome   shang   san   

原文地址:http://www.cnblogs.com/acer-haitao/p/7263679.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!