学习笔记-小甲鱼Python3学习第二十五讲:字典:当索引不好用时

字典是一组{}大括号括起来的键、值组合。

键(key)

值(value)

映射

  A   B

|---|     |---|

| a |-->| m |

| b |-->| n |

| c |-->| p |

| d |-->| q |

|---|     |---| 


创建和访问字典

>>> dict1 = { '李宁':'一切皆有可能','耐克':'Just do it','阿迪达斯':'Impossible is nothing','小伙子':'看好你哟'}                                                     #使用{}直接创建

>>> print('小伙子的口号是:',dict1['小伙子'])

小伙子的口号是: 看好你哟

>>> dict3 = dict((('Lilie',98),('Han×××',99),('Lucy',97),('Lily',99)))        #使用dict()函数键值对创建

>>> dict3

{'Lilie': 98, 'Han×××': 99, 'Lily': 99, 'Lucy': 97}

>>> print(dict3['Han×××'])                                                 #查找键值

99

>>> dict4 = dict(Python='牛逼的语言',php='最好的语言')                   #使用dict()函数赋值方式创建字典

>>> dict4

{'Python': '牛逼的语言', 'php': '最好的语言'}

>>> dict4['php']

'最好的语言'

>>> dict4['php'] = '最好的吗?呵呵'                           #修改值

>>> dict4

{'Python': '牛逼的语言', 'php': '最好的吗?呵呵'}

>>> dict4['C'] = '基础的基础'                                 #新增键值

>>> dict4

{'C': '基础的基础', 'Python': '牛逼的语言', 'php': '最好的吗?呵呵'}


------------------分割线,哈哈哈--------------------


课后作业:


0.当你听到小伙伴们在谈论“映射”、“哈希”、“散列”或者“关系数组”的时候,事实上他们就是在讨论什么呢?

全都是字典


1.尝试一下将数据('F':70,'C':67,'h':104,'i':105,'s':115)创建为一个字典并访问键‘C’对应的值。

dict5 = dict((('F',70),('C',67),('h',104),('i',105),('s',115)))

>>> dict5['C']

67

>>> print(dict5['C'])

67


2.用方括号‘[’括起来的数据我们叫列表,那么使用大括号‘{’括起来的数据我们就叫字典,对吗?

不对。用大括号括起来的数据如果没有映射关系则不是字典


3.你如何理解有些事情字典做得到,但是“万能的”列表却难以实现?

数据之间有关联关系,如姓名和分数,如果用列表得定义两个列表。使用姓名查找分数需要在两个列表中操作,效率低下。使用字典却方便很多


4.下边这些代码,他们都在执行一样的操作吗?你看得出差别吗?


>>> 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)])

>>> d = dict({'three':3,'one':1,'two':2})

一样的,都是创建字典{'one': 1, 'two': 2, 'three': 3}


5.如图,你可以推测出打了马赛克部分的代码吗?


data = "1000,小甲鱼,男"

MyDict = {}

(MyDict['id'],MyDict['name'],MyDict['sex']) = data.split(',')

print("ID:" + MyDict['id'])

print("Name:" + MyDict['name'])

print("Sex:" + MyDict['sex'])


分别把data里面的值分片赋给字典MyDict里面的键。data.split(',')


----------------------分割线,哈哈哈---------------------


动动手:


0.尝试利用字典的特性编写一个通讯录程序吧。

print("The contact program.")

print("1.Search the contactor.")

print("2.Insert the contactort.")

print("3.Del the contactor.")

print("4.Quit.")


contacts = {'Lilei':27899403,'Han×××':456243792,'Lily':23473650,'Luvy':10374923}


while 1:

    temp = int(input("Please input your choice.num:"))

    if temp == 1:

        name = input("Please input the name:")

        if name in contacts.keys():

            print(contacts[name])

        else:

            print("No such contactor.")


    if temp == 2:

        name = input("Please input the name:")

        Tel = input("Please input the Tel:")

        contacts[name] = Tel


    if temp == 3:

        name = input("Please input the del name:")

        if name in contacts.keys():

            contacts.pop(name)

        else:

            print("No such contactor.")


    if temp == 4:

        break


猜你喜欢

转载自blog.51cto.com/wszzdanm/2169912