1、字典中键-值为一对,keys()返回一个列表,包含字典中所有键,values()返回所有值
favorite_languages ={ 'jack':"python", 'alex':"c", 'james':"ruby" } for key in favorite_languages.keys(): print(key) print(list(favorite_languages.keys())) for value in favorite_languages.values(): print(value) print(list(favorite_languages.values()))
2、给字典添加键-值对
favorite_languages ={ 'jack':"python", 'alex':"c", 'james':"ruby" } favorite_languages['paul']='c++' favorite_languages['charlie']='c' print('favorite_language is :'+str(favorite_languages['charlie'])) print(favorite_languages['paul']) print("Paul's favorite_language is %s."%favorite_languages['paul'])
3、items()返回一个键—值对列表;
favorite_languages ={ 'jack':"python", 'alex':"c", 'james':"ruby" } for k,v in favorite_languages.items(): print(' K:'+k) print('V:'+v)
4、keys()--键列表的应用
#遍历键列表 for key in favorite_languages.keys(): print(' '+key.title()) #检查一个朋友是否在键列表中 friends=['jack','james'] for name in favorite_languages.keys(): print(' '+name.title()) if name in friends: print('Hi '+name.title()+',I see your favorite language is '+ favorite_languages[name].title() + '!') else: print(name.title(),',please take our poll.') #按顺序遍历字典中所有键 for name in sorted(favorite_languages.keys()): print(name.title())
5、set()集合的方式输出列表,集合中每个元素必须独一无二
#values()值列表应用 favorite_languages ={ 'jack':"python", 'alex':"c", 'james':"c" } languages=[] for language in set(favorite_languages.values()): print(language.title()) languages.append(language) print(languages)
6、嵌套:列表中嵌套字典
#将两个字典循环插入空列表30次 resumes=[] for resume_number in range(30): new_resume1={'name':'jack','age':'23','living_city':'beijing'} new_resume2={'name':'alex','age':'26','living_city':'shanghai'} resumes.append(new_resume1) resumes.append(new_resume2) for resume in resumes[:3]: print(resume)#依次输出前三个字典 if resume['name'] == 'jack':#按条件修改字典中的值 resume['name']='james' resume['age']='30' resume['living_city']='zhengzhou' elif resume['name'] == 'alex': resume['name']='paul' resume['age']='35' resume['living_city']='shenzhen' print(resume)#依次输出修改后的前三个字典 print('The number of new resumes is %s.'%str(len(resumes)))#输出列表长度
7、字典中嵌套列表
#字典的值为列表 favorite_flowers={ 'jack':['rose','lily','peony'], 'alex':'lily', 'james':['lily','peony','tulip'] } for name,flowers in favorite_flowers.items():#遍历字典 print(' ',len(flowers))#输出值的长度 if len(flowers) > 1:#如果长度大于1,就为列表 print(name.title()+"'s favorite flower are:") for flower in flowers: print(' ',flower.title()) else: #否则就是单个字符串 print(name.title()+"'s favorite flower is:") print(' ',flowers[0].title())
8、字典中嵌套字典
#字典的值为字典 people={ 'eric':{'name':'eric','age':'25','location':'beijing'}, 'james':{'name':'james','age':'22','location':'shanghai'} } for person,info in people.items():#遍历字典 print(' Username:',person) #依次输出键 for k,v in info.items(): #遍历嵌套字典的键值对 print('%s:%s'%(k,v)) print('------------')
9、setdefault()--括号内的值可以是key活着key,value,如果字典中有key,输出对应的值,如果括号内的key不存在,就给字典添加一个key,value值默认为None;
这个函数尽量不要用,容易出错
user_info = { 'jack':'c', 'alex':'c++' } print(user_info.setdefault('jack')) print(user_info.setdefault('james','python')) print(user_info)
10、fromkeys()只是借用一个字典的格式来生成一个新的字典,不改变借用字典,这个函数尽量不要用
popitem() 随机删除字典中的键-值对;尽量不要用
user_info = { 'jack':'c', 'alex':'c++' } print(user_info.fromkeys([1,2,3,4],'aaaa')) print(user_info) print(user_info.popitem())#随机删除字典中的键-值对;尽量不要用 print(user_info)
11、update()根据新的字典更新老的字典,已存在的键值对,根据第二个字典改变,没有的就添加进来,一切以第二个为准
user_info = { 'jack':'c', 'alex':'c++' } user_info2 = { 'alex':'python', 'paul':'java' } user_info.update(user_info2)#update()根据新的字典更新老的字典 print(user_info)
12、dic.get(key)函数获取字典的value,如果key不存在,就返回None,不会报错,如果直接用dic[key]=value,如果字典为空,就会报错。
dic = {} print(dic.get("key")) #None