首页 > 技术文章 > Python学习第五篇——如何访问字典

shaonianpi 2018-08-13 10:01 原文

 1 # the example_1 aim to tell how to use dctionary,and how to access list or dictionary
 2 infos={"first_name":"jack","last_name":"mole","age":20,"city":"lendon"}
 3 for  info  in infos:
 4     print(str(infos[info]))
 5 # example2 aim to how to access dictionary's key and value
 6 friends_num = {"wang":8,"li":0,"jack":3,"mole":6,"zhao":5}
 7 for friend_num in friends_num:
 8     #print(friend_num)
 9     print("my friend "+friend_num+
10         " like "+str(friends_num[friend_num]))
11     

1 上述代码的作用在于介绍字典的概念:字典的核心概念在于:键——值,这样的对应关系,字典并不关心键和键的顺序关系,只关心键——值的对应关系

2 正确访问字典:字典名[键]——访问值,同时通过程序2中对键本身的访问,再次理解访问 列表或者字典时,for关键字后面的变量究竟意味着什么??

 

推荐阅读