alien_0={'color':'green','point':5} print(alien_0) del alien_0['color'] print(alien_0)
1 2
{'color': 'green', 'point': 5} {'point': 5}
来一个大家伙
1 2 3 4 5 6 7 8
favorite_language ={ 'edward':'rudy', 'jen':'python', 'phil':'python', 'sarah':'c', } language = favorite_language['sarah'].title() print(f"sarah's favorite language is {language}")
favorite_languages = { 'edward': 'rudy', 'jen': 'python', 'phil': 'python', 'sarah': 'c', } friends = ['sarah','jen'] //这里的“,”很关键,这是分隔开两个的, 如果没有的话会默认两个连接在一起输出 for name in favorite_languages.keys(): print(f"{name.title()}")
if name in friends: language = favorite_languages[name].title() print(f"{name.title()},i know you like {language}")
1 2 3 4 5 6 7
Edward Jen Jen,i know you like Python Phil Sarah Sarah,i know you like C
key()方法:返回一个字典所有的键。
按照特定顺序来遍历字典中的所有键
1 2 3 4 5 6 7 8 9 10
from builtins import print
favorite_languages = { 'jen': 'python', 'phil': 'python', 'edward': 'rudy', 'sarah': 'c', } for name in sorted(favorite_languages.keys()): print(f"{name.title()},thanks for you ")
1 2 3 4
Edward,thanks for you Jen,thanks for you Phil,thanks for you Sarah,thanks for you
set()可以找出独一无二的元素
1 2 3 4 5 6 7 8 9 10
from builtins import print
favorite_languages = { 'jen': 'python', 'phil': 'python', 'edward': 'rudy', 'sarah': 'c', } for language in set(favorite_languages.values()): print(f"{language.title()}")
1 2 3
Rudy Python C
keys() 方法用于返回字典中的所有键;values() 方法用于返回字典中所有键对应的值
嵌套
1 2 3 4 5 6 7
alien_00={'color':'green','points':5} alien_01={'color':'blue','points':6} alien_02={'color':'red','points':9} alien_03={'color':'yellow','points':7} aliens=[alien_00,alien_01,alien_02,alien_03] for alien in aliens: print(alien)
aliens = [] for alien_number in range(20): new_alien = {'color':'green','point':5} aliens.append(new_alien)
for alien in aliens[:3]: if alien['color']=='green': alien['color']='yellow' alien['point']='10' elif alien['color']=='yellow': alien['color']='red' alien['point']='20' for alien in aliens[:5]: print(alien)
pizza = { 'sort':'好好好', 'materials':['好','好个寂寞好'], } print(f"you order a {pizza['sort']} pizza") for material in pizza['materials']: print('\t'+material)