学习目标:字典和集合
学习内容:
字典:
字典是一种可变容器模型,且可储存任意类型对象。也是一种映射。
形如:d = {key1:value1,key2:value2}必须有这样的映射关系才能成为字典。
其中key为键 value为值。
键一般是唯一的,如果重复后面的键会替换前面的键,值不需要唯一。
>>> dict={'a':1,'b':2,'b':5}
>>> dict['b']
5
>>> dict
{'a': 1, 'b': 5}
如果试图为一个不存在的键(key)赋值会自动创建对应的键并添加相应的值(value)进去。
```python
>>> dict2={1:'小明',2:'小红',3:'小李'}
>>> dict2[4]='小王'
>>> dict2
{1: '小明', 2: '小红', 3: '小李', 4: '小王'}
使用字典的方式编写一个通讯录程序;
def check():
name = input('请输入想要查询的名字:')
if name in dictionary:
print(dictionary[name])
else:
print('该联系人不存在')
def add():
name = input('想要修改或添加的联系人:')
if name in dictionary:
print('该联系人已存在:',dictionary[name])
action = input('修改请按1,退出请按2')
if action == '1':
tel=input('姓名:')
dictionary[name]=tel
print('修改完成')
else:
tel=input('请输入需要添加的联系人电话;')
dictionary[name]=tel
print('添加完成')
def delete():
action = input('删除单个联系人请按1,全部清空请按2,其余健退出:')
if action == '1':
name = input('想要删除的联系人:')
if name in dictionary:
del(dictionary[name])
else:
print('该联系人不存在')
elif action == '2':
dictionary.clear()
print('通讯录已全部清空')
def checkall():
print(dictionary)
if len(dictionary):
for name,tel in dictionary.items():
print(name,tel)
else:
print('没有联系人')
print('------------欢迎进入通讯录系统:------------')
print('------------需要查询请按1-------------------')
print('------------添加或修改联系人请按2-----------')
print('------------删除联系人请按3-----------------')
print('------------查看全部通讯录请按4-------------')
print('------------退出请按5-----------------------')
dictionary = {}
while True:
action = input('请输入要精选操作的选项:')
if action == '1':
check()
if action == '2':
add()
if action == '3':
delete()
if action == '4':
checkall()
if action == '5':
break
while action not in ('1','2','3','4','5'):
print('输入有误,请重新输入:')
action = input('请输入要精选操作的选项:')
if action == '1':
check()
if action == '2':
add()
if action == '3':
delete()
if action == '4':
checkall()
if action == '5':
break
字典的各种内置方法:
dict.clear() | 删除字典内所有元素 |
---|---|
dict.copy() | 返回一个字典的浅复制 |
dict.get() | 返回指定键的值,如果值不存在则返回default |
dict.items() | 以列表返回一个字典所有的(键,值) |
dict.keys() | 以列表返回一个字典所有的键 |
dict.values() | 以列表返回一个字典所有的值 |
dict.pop() | 给定键弹出相应的值 |
dict.1update(dict2) | 把字典dict2里的键和值更新到dict1里 |
dict.popitem() | 返回并删除字典中的最后一对键和值 |
编写一个用户登录程序:
效果如下:
print('------新建用户:N/n------')
print('------登录用户:E/e------')
print('------退出程序:Q/q------')
def f():
dict1={}
while True:
print('请输入指令:',end='')
demand=input()
if demand == 'N' or demand=='n':
name=input('请输入新建用户名:')
while name in dict1:
name=input('此用户名已被使用,请重新输入用户名:')
dict1[name]=input('请输入新建密码')
print('注册成功,现在尝试登陆吧')
continue
elif demand == 'E' or demand=='e':
name=input('请输入用户名:')
while name not in dict1:
name=input('此用户名不存在,请重新输入用户名:')
a=input('请输入密码')
while a!=dict1[name]:
a=input('密码错误,请重新输入')
print('欢迎进入系统,退出请按Q/q')
continue
elif demand == 'Q' or demand=='q':
break
f()
集合:
集合的定义:
>>> num={1:'a',2:'b'}
>>> type(num)
<class 'dict'>
>>> num1={1,2,3}
>>> type(num1)
<class 'set'>
集合内不可能存在两个相同的元素,且默认按从小到大的顺序排序。
>>> num2={1,4,2,6,9,8,9,0}
>>> num2
{0, 1, 2, 4, 6, 8, 9}
如果要保持顺序不变。
>>> set = set([1,5,9,0,0,7])
>>> set
{0, 1, 5, 7, 9}
>>> temp=[]#申明成列表的形式
>>> num5={1,2,3,7,6,6,8,9,0}
>>> for each in num5:
if each not in temp:
temp.append(each)
>>> temp
[0, 1, 2, 3, 6, 7, 8, 9]
给集合添加或删减元素,也适用于s.clear() s.pop() s.update等方法。
>>> num5.add(10)
>>> num5
{0, 1, 2, 3, 6, 7, 8, 9, 10}
>>> num5.remove(10)
>>> num5
{0, 1, 2, 3, 6, 7, 8, 9}
frozen 冰冻集合使之不可改变。
>>> num1=frozenset([1,2,3,5,2])
>>> num1
frozenset({1, 2, 3, 5})
>>> num1.add(10)
Traceback (most recent call last):
File "<pyshell#109>", line 1, in <module>
num1.add(10)
AttributeError: 'frozenset' object has no attribute 'add'