#字典在python里是一系列的键值对,每个键对应一个值,可以通过键来访问值,这里的值可以是:数字,字符串。列表,乃至字典, #字典放在{}花括号中的一系列键值对表示 diction = {"color" : "green","age" : 24} print(diction["age"]) #24
#添加键值对,python你在乎添加键值对的顺序,他只在乎添加的键值对的内容 diction["x_position"] = 0 diction["y_position"] = 25 print(diction)
#修好对应键的值 diction_1 = {} diction_1["x_position"] = 0 diction_1["y_position"] = 25 diction_1["speed"] = "medium" if diction_1["speed"] == "slow": x_increment = 1 elif diction_1["speed"] == "medium": x_increment = 2 else: x_increment = 2 diction_1["x_position"] = diction_1["x_position"] + x_increment print("现在的x的位置是:",diction_1["x_position"])
#删除键值对,使用del 语法是:del 字典名["键"],删除后的键值对就永远消失了 diction = {'x_position':12,'y_position':23} print("删除之前:",diction) del diction['x_position'] print("删除之后:",diction)
以上都是完成一个对象的所有信息调查,接下来可以是多个对象的调查:
#除了可以把字典写成一行,还可以写成多行 people_languages = { '大怪':'C', '小怪':'C++', '八戒':'Java', '唐生':'Python', } print('八戒最喜欢的语言是:'+people_languages['八戒'].title()+'.') #八戒最喜欢的语言是:Java.
#遍历字典的所有键值对 people_languages = { '大怪':'C', '小怪':'C++', '八戒':'Java', '唐生':'Python', } #使用到了字典的方法item(),这个方法返回的是键值对列表 for key,value in people_languages.items(): print('KEY: '+key+'\n'+'VALUE: '+value)
people_languages = { '大怪':'C', '小怪':'C++', '八戒':'Java', '唐生':'Python', } for name in people_languages.keys(): print(name) # 大怪 # 小怪 # 八戒 # 唐生
people_languages = { '大怪':'C', '小怪':'C++', '八戒':'Java', '唐生':'Python', } frinedName = ['孙悟空','蜘蛛精','小怪','唐生'] for name in people_languages.keys(): if name in frinedName: print('Hi '+name+',i have see you in frindName') else: continue # Hi 小怪,i have see you in frindName # Hi 唐生,i have see you in frindName
#按照sorted来排序打印 people_languages = { '大怪':'C', '小怪':'C++', '八戒':'Java', '唐生':'Python', } for name in sorted(people_languages.keys()): print(name,end=" ") print() for name in sorted(people_languages.keys(),reverse=True): print(name,end=" ") # 八戒 唐生 大怪 小怪 # 小怪 大怪 唐生 八戒
#考虑到值得重复问题 people_languages = { '大怪':'C', '小怪':'C++', '八戒':'Java', '唐生':'Python', '哪吒':'C++', } #只打印出所有的值,无论重复与否 for value in people_languages.values(): print(value,end=" ") #C C++ Java Python C++ print() #考虑值得重复,不打印重复的值,要用到set for value in set(people_languages.values()): print(value,end=" ") #C Java C++ Python
#在列表中存放字典 aliens = [] for alien in range(0,5): new_alien = {'name':'bajie','age':12} aliens.append(new_alien) for alien in aliens[:2] : if alien['name'] == 'bajie': alien['name'] = 'feiwu' alien['age'] = 19 for alien in aliens : print(alien) # {'name': 'feiwu', 'age': 19} # {'name': 'feiwu', 'age': 19} # {'name': 'bajie', 'age': 12} # {'name': 'bajie', 'age': 12} # {'name': 'bajie', 'age': 12}
#在字典中添加列表 Languages_friend = { 'xiaoming':['python','C++'], 'xiaohua':['python','C'], 'cuihua':'Java', 'yuejie':['Qt','ruby'], } #切记这里必须是.item(), for name,languages in Languages_friend.items(): print("name : "+name,end="\t") for language in languages: print("language : ",language.title(),end=" ") print()
#在字典中存放字典 Users = { 'xiaoming':{ 'age':24, 'sex':'man', 'favorite':'basketball', 'province':'shanxi', }, #不要忘记这里有一个逗号 'cuihua':{ 'age':25, 'sex':'woman', 'favorite':'singing', 'province':'guangxi', } } for name,userInfo in Users.items(): print("name : ",name) for key,value in userInfo.items(): print(key," ",value)