标签:lin tle 必须 表示 phi mos ati values following
alien_0 = {‘color‘:‘green‘,‘points‘: 5} print(alien_0[‘color‘]) print(alien_0[‘points‘]) #结果如下: #green #5
在Python中,字典是一系列键值对。每个键都与一个值相关联,你可以使用键来访问相关联的值。字典用放在花括号{}中的一系列键值对表示。
alien_0 = {‘color‘:‘green‘,‘points‘: 5}
要获取与键相关联的值,可依次指定字典名和放在方括号内的键。
alien_0 = {‘color‘:‘green‘,‘points‘: 5} print(alien_0[‘color‘]) #结果如下: #5
alien_0 = {‘color‘:‘green‘,‘points‘: 5} new_points = alien_0[‘points‘] print(f"You just earned {new_points} points!") #结果如下: #You just earned 5 points!
字典是一种动态结构,可随时在其中添加键值对。要添加键值对,可依次指定字典名、用方括号括起来的键和相关联的值。
alien_0 = {‘color‘:‘green‘,‘points‘: 5} print(alien_0) alien_0[‘x_position‘] = 0 alien_0[‘y_position‘] = 25 print(alien_0) #结果如下: #{‘color‘:‘green‘,‘points‘: 5} #{‘color‘: ‘green‘, ‘points‘: 5, ‘x_position‘: 0, ‘y_position‘: 25}
alien_0 = {} alien_0[‘color‘] = ‘green‘ alien_0[‘points‘] = 5 print(alien_0) #结果如下: #{‘color‘: ‘green‘, ‘points‘: 5}
要修改字典中的值,可依次指定字典名、用方括号括起的键,以及与该键相关联的值。
alien_0 = {‘color‘:‘green‘} print(f"The alien is {alien_0[‘color‘]}.") alien_0[‘color‘] = ‘yellow‘ print(f"The alien is now {alien_0[‘color‘]}.") #结果如下: #The alien is green. #The alien is now yellow.
alien_0 = {‘x_position‘:0,‘y_position‘:25,‘speed‘:‘medium‘} print(f"Original position: {alien_0[‘x_position‘]}") #向右移动外星人 #根据当前速度确定将外星人向右移动多远。 if alien_0[‘speed‘] == ‘slow‘: x_increment = 1 elif alien_0[‘speed‘] == ‘medium‘: x_increment = 2 else: #这个外星人的移动速度肯定很快。 x_increment = 3 #新位置为旧位置加上移动距离。 alien_0[‘x_position‘] = alien_0[‘x_position‘] + x_increment print(f"New position:{alien_0[‘x_position‘]}") #结果如下: #Original position: 0 #New position:2
可使用del语句将对应的键值对彻底删除,使用del语句时,必须指定字典名和要删除的键。
alien_0 = {‘color‘:‘green‘,‘points‘: 5} print(alien_0) del alien_0[‘points‘] print(alien_0) #结果如下: #{‘color‘: ‘green‘, ‘points‘: 5} #{‘color‘: ‘green‘}
favorite_languages ={ ‘jen‘: ‘python‘, ‘sarah‘: ‘c‘, ‘edward‘: ‘ruby‘, ‘phil‘: ‘python‘, } language =favorite_languages[‘sarah‘].title() print(f"Sarah‘s favorite language is {language}.") #结果如下: #Sarah‘s favorite language is C.
alien_0 = {‘color‘:‘green‘,‘speed‘: ‘slow‘} print(alien_0[‘points‘])
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
print(alien_0[‘points‘])
KeyError: ‘points‘
方法get()的第一个参数用于指定键,是必不可少的;第二个参数为指定的键不存在是要返回的值,是可选的:
alien_0 = {‘color‘:‘green‘,‘speed‘: ‘slow‘} point_value = alien_0.get(‘points‘,‘No point value assigned.‘) print(point_value) #结果如下: #No point value assigned.
调用get()时,如果没有指定第二个且指定的键不存在,Python将返回值None。
for k,v in user_0.items()
user_0 ={ ‘usesrname‘:‘efermi‘, ‘first‘:‘enrico‘, ‘last‘:‘feermi‘, } for key,value in user_0.items(): print(f"\nkey:{key}") print(f"value:{value}") #结果如下: #key:usesrname #value:efermi #key:first #value:enrico #key:last #value:feermi
favorite_languages ={ ‘jen‘: ‘python‘, ‘sarah‘: ‘c‘, ‘edward‘: ‘ruby‘, ‘phil‘: ‘python‘, } for name,language in favorite_languages.items(): print(f"{name.title()}‘s favorite language is {language.title()}.") #结果如下: #Jen‘s favorite language is Python. #Sarah‘s favorite language is C. #Edward‘s favorite language is Ruby. #Phil‘s favorite language is Python.
在不需要使用字典中的值时,方法keys()很有用。
favorite_languages ={ ‘jen‘: ‘python‘, ‘sarah‘: ‘c‘, ‘edward‘: ‘ruby‘, ‘phil‘: ‘python‘, } for name in favorite_languages.keys(): print(name.title()) #结果如下: #Jen #Sarah #Edward #Phil
favorite_languages ={ ‘jen‘: ‘python‘, ‘sarah‘: ‘c‘, ‘edward‘: ‘ruby‘, ‘phil‘: ‘python‘, } friends =[‘phil‘,‘sarah‘] for name in favorite_languages.keys(): print(f"Hi,{name.title()}.") if name in friends: language = favorite_languages[name].title() print(f"\t{name.title()},I see you love {language}.") #结果如下: #Hi,Jen. #Hi,Sarah. # Sarah,I see you love C. #Hi,Edward. #Hi,Phil. # Phil,I see you love Python.
favorite_languages ={ ‘jen‘: ‘python‘, ‘sarah‘: ‘c‘, ‘edward‘: ‘ruby‘, ‘phil‘: ‘python‘, } if ‘erin‘ not in favorite_languages.keys(): print("Erin please take our poll!") #结果如下: #Erin please take our poll!
favorite_languages ={ ‘jen‘: ‘python‘, ‘sarah‘: ‘c‘, ‘edward‘: ‘ruby‘, ‘phil‘: ‘python‘, } for name in sorted(favorite_languages.keys()): print(f"{name.title()},thank you for taking the poll.") #结果如下: #Edward,thank you for taking the poll. #Jen,thank you for taking the poll. #Phil,thank you for taking the poll. #Sarah,thank you for taking the poll.
favorite_languages ={ ‘jen‘: ‘python‘, ‘sarah‘: ‘c‘, ‘edward‘: ‘ruby‘, ‘phil‘: ‘python‘, } print("The following languages have been mentioned:") for language in favorite_languages.values(): print(language.title()) #结果如下: #The following languages have been mentioned: #Python #C #Ruby #Python
如果被调查者很多,最终的列表可能包含大量重复项。为剔除重复项,可使用集合(set),集合中的每一个元素都必须是独一无二的。
favorite_languages ={ ‘jen‘: ‘python‘, ‘sarah‘: ‘c‘, ‘edward‘: ‘ruby‘, ‘phil‘: ‘python‘, } print("The following languages have been mentioned:") for language in set(favorite_languages.values()): print(language.title()) #结果如下: #The following languages have been mentioned: #C #Ruby #Python
可使用一对花括号直接创建集合,并在其中用逗号分隔元素:
languages ={‘python‘,‘ruby‘,‘python‘,‘c‘} #结果如下: #{‘c‘, ‘ruby‘, ‘python‘}
有时候,需要将一系列字典存储在列表中,或将列表作为值存储在字典中,这称为嵌套。
alien_0 = {‘color‘:‘green‘,‘points‘:5} alien_1 = {‘color‘:‘yellow‘,‘points‘:10} alien_2 = {‘color‘:‘red‘,‘points‘:15} aliens =[alien_0,alien_1,alien_2] for alien in aliens: print(alien) #结果如下: #{‘color‘: ‘green‘, ‘points‘: 5} #{‘color‘: ‘yellow‘, ‘points‘: 10} #{‘color‘: ‘red‘, ‘points‘: 15}
使用range()生成了30个外星人
#创建一个用于存储外星人的空列表 aliens = [] #创建30个绿色的外星人 for alien_number in range(30): new_alien = {‘color‘:‘green‘,‘point‘:5,‘speed‘:‘slow‘} aliens.append(new_alien) #显示前5个外星人 for alien in aliens[:5]: print(alien) print("...") #显示创建了多少个外星人 print(f"Total number of aliens:{len(aliens)}") #结果如下: #{‘color‘: ‘green‘, ‘point‘: 5, ‘speed‘: ‘slow‘} #{‘color‘: ‘green‘, ‘point‘: 5, ‘speed‘: ‘slow‘} #{‘color‘: ‘green‘, ‘point‘: 5, ‘speed‘: ‘slow‘} #{‘color‘: ‘green‘, ‘point‘: 5, ‘speed‘: ‘slow‘} #{‘color‘: ‘green‘, ‘point‘: 5, ‘speed‘: ‘slow‘} #... #Total number of aliens:30
修改前三个外星人颜色为黄色,速度为中等,值为10分
#创建一个用于存储外星人的空列表 aliens = [] #创建30个绿色的外星人 for alien_number in range(30): new_alien = {‘color‘:‘green‘,‘point‘:5,‘speed‘:‘slow‘} aliens.append(new_alien) for alien in aliens[:3]: if alien[‘color‘] ==‘green‘: alien[‘color‘] =‘yellow‘ alien[‘point‘] =10 alien[‘speed‘] =‘medium‘ #显示前5个外星人 for alien in aliens[:5]: print(alien) print("...") #结果如下: #{‘color‘: ‘yellow‘, ‘point‘: 10, ‘speed‘: ‘medium‘} #{‘color‘: ‘yellow‘, ‘point‘: 10, ‘speed‘: ‘medium‘} #{‘color‘: ‘yellow‘, ‘point‘: 10, ‘speed‘: ‘medium‘} #{‘color‘: ‘green‘, ‘point‘: 5, ‘speed‘: ‘slow‘} #{‘color‘: ‘green‘, ‘point‘: 5, ‘speed‘: ‘slow‘} #...
列表中存储披萨的两方面信息:外皮类型和配料列表
配料列表是一个与键‘toppings’相关联的值
#存储所点披萨的信息 pizza = { ‘crust‘:‘thick‘, ‘toppings‘:[‘mushrooms‘,‘extra cheese‘], } #概述所点的披萨 print(f"You ordered a {pizza[‘crust‘]}-crust pizza with the fllowing toppings:") for topping in pizza[‘toppings‘]: print("\t"+topping) #结果如下: #You ordered a thick-crust pizza with the fllowing toppings: # mushrooms # extra cheese
favorite_languages = { ‘jen‘:[‘python‘,‘ruby‘], ‘sarah‘:[‘c‘], ‘edward‘:[‘ruby‘,‘go‘], ‘phil‘:[‘python‘,‘haskell‘] } for name,languages in favorite_languages.items(): print(f"\n{name.title()}‘s favorite languages are:") for language in languages: print(f"\t{language.title()}") #结果如下: #Jen‘s favorite languages are: # Python # Ruby #Sarah‘s favorite languages are: # C #Edward‘s favorite languages are: # Ruby # Go #Phil‘s favorite languages are: # Python # Haskell
user = { ‘aeinstein‘:{ ‘first‘:‘albert‘, ‘last‘:‘aeinstein‘, ‘location‘:‘princeton‘, }, ‘mcurie‘:{ ‘first‘:‘marie‘, ‘last‘:‘curie‘, ‘location‘:‘paris‘, }, } for username,user_info in user.items(): print(f"\nUsername:{username}") full_name = f"{user_info[‘first‘]} {user_info[‘last‘]}" location = user_info[‘location‘] print(f"\tFull_name:{full_name.title()}") print(f"\tLocation: {location.title()}") #结果如下: #Username:aeinstein # Full_name:Albert Aeinstein # Location: Princeton #Username:mcurie # Full_name:Marie Curie # Location: Paris
标签:lin tle 必须 表示 phi mos ati values following
原文地址:https://www.cnblogs.com/nana12138/p/14518769.html