关于字典
一. 课上代码
>>> brand = ['李宁','耐克','阿迪达斯','fishc']
>>> slogan = ['一切皆有可能', 'Just do it', 'Impossible in nothing', 'Let programming changes the world']
>>> print('the slogan of fishc is: ', slogan[brand.index('fishc')])
the slogan of fishc is: Let programming changes the world
#注意集合使用的是大括号
>>> dict1 = {'李宁','耐克','阿迪达斯','fishc'}
>>> dict1 = {'李宁': '一切皆有可能','耐克': 'Just do it','阿迪达斯': 'Impossible in nothing','fishc': 'Let programming changes the world'}
>>> print('the slogan of fishc is: ',dict1['fishc'])
the slogan of fishc is: Let programming changes the world
>>> dict2 = {1:'one', 2: 'two', 3: 'three'}
>>> dict2[2]
'two'
>>> dict3 = {}
>>> dict3
{}
>>> dict3 = dict((('F', 70), ('i', 105), ('h', 104), ('C', 67)))
#此处需要用两个括号
>>> dict3
{'F': 70, 'i': 105, 'h': 104, 'C': 67}
>>> dict3 = dict(('F', 70), ('i', 105), ('h', 104), ('C', 67))
Traceback (most recent call last):
File "", line 1, in
dict3 = dict(('F', 70), ('i', 105), ('h', 104), ('C', 67))
TypeError: dict expected at most 1 arguments, got 4
#如何少了一个那就会报错
>>> dict3 = dict((['F', 70], ['i', 105], ['h', 104], ['C', 67]))
>>> dict3
{'F': 70, 'i': 105, 'h': 104, 'C': 67}
#括号里面用元组或者用列表都是可以的
>>> dict4 = dict(UNSW = 'U never sleep well', USYD = 'U sleep you die')
>>> dict4
{'UNSW': 'U never sleep well', 'USYD': 'U sleep you die'}
>>> dict4 = dict('UNSW' = 'U never sleep well', USYD = 'U sleep you die')
SyntaxError: keyword can't be an expression
#此处注意,key不能加引号,因为key不能是一个表达式
>>> dict4['USYD'] = 'Worse than UNSW'
>>> dict4
{'UNSW': 'U never sleep well', 'USYD': 'Worse than UNSW'}
#这里是可以对集合数据进行改变的
>>> dict4['UTS'] = 'The best IT school in Sydney'
>>> dict4
{'UNSW': 'U never sleep well', 'USYD': 'Worse than UNSW', 'UTS': 'The best IT school in Sydney'}
#如何是一个新的元素,那么就会加入到集合之中
>>>
二. 测试题
- 当谈论到“映射”,“哈希”,“散列”或者“关系数组”的时候,分别代表什么?
#都是一个概念,就是这一讲中的“字典”
- 尝试将数据('F': 70, 'C': 67, 'h': 104, 'i': 105, 's': 115)创建一个字典并访问键'C'对应的值
>>> dict1 = dict((('F', 70), ('C', 67), ('h', 104), ('i', 105), ('s', 115)))
>>> dict1['C']
67
- 用方括号("[]")括起来的数据叫做列表,那么使用大括号("{}")括起来的数据我们就叫字典,对吗?
#并不是
>>> NotADict = {1, 2, 3, 4, 5}
>>> type(NotADict)
- 下边这些代码,他们都在执行一样的操作嘛?
>>> a = dict(one = 1, two = 2, three = 3)
>>> b = {'one':1, 'two': 2, 'three': 3}
>>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
>>> d = dict([('two', 2), ('one', 1), ('three', 3)])
>>> e = dict({'three': 3, 'one': 1, 'two': 2})
#是的,都是在创建字典
>>> a
{'one': 1, 'two': 2, 'three': 3}
>>> b
{'one': 1, 'two': 2, 'three': 3}
>>> c
{'one': 1, 'two': 2, 'three': 3}
>>> d
{'two': 2, 'one': 1, 'three': 3}
>>> e
{'three': 3, 'one': 1, 'two': 2}
- 非常重要的一个功能
data = "Jane, F, 18"
MyDict = {}
(MyDict['name'], MyDict['sex'], MyDict['age']) = data.split(",")
print("Name: " + MyDict['name'])
print("Sex: " + MyDict['sex'])
print("Age: " + MyDict['age'])
>>>
================== RESTART: C:/Users/Xinqi/Desktop/test.py ==================
Name: Jane
Sex: F
Age: 18
三. 动动手
-
尝试利用字典的特性写一个通讯录程序,功能如图
print("|---欢迎进入通讯录程序---|")
print("|---1:查询联系人资料 ---|")
print("|---2:插入新的联系人 ---|")
print("|---3:删除已有联系人 ---|")
print("|---4:退出通讯录程序 ---|")
contacts = dict()
while 1:
instr = int(input("\n请输入相关的指令代码:"))
if instr == 1:
name = input("请输入联系人姓名:")
if name in contacts:
print(name + ' : ' + contacts[name])
else:
print("您输入的姓名不在通讯录中!")
if instr == 2:
name = input("请输入联系人姓名:")
if name in contacts:
print("您输入的姓名在通讯录中已存在 -->> ", end = '')
print(name + ' : ' + contacts[name])
choice = input("是否修改用户资料(YES/NO):")
if choice == 'YES':
phone_number = input("请输入用户联系电话:")
contacts[name] = phone_number
else:
contacts[name] = input("请输入用户联系电话:")
if instr == 3:
name = input("请输入联系人姓名:")
if name in contacts:
del(contacts[name])
print("操作成功!")
else:
print("您输入的联系人不存在。")
if instr == 4:
break
print("|--- 感谢使用通讯录程序 ---|")