赞
踩
alien_0 ={'color':'green'}
要获取与键相关联的值,可依次指定字典名、放在方括号内的键名
alien_0 ={'color':'green','points':5}
print(alien_0['color'])
print(alien_0['points'])
green
5
>>> alien_0 ={'color':'green','points':5}
>>> new_points = alien_0['points']
>>> print("You just earned "+str(new_points)+" points!")
You just earned 5 points!
>>> alien_0 ={'color':'green','points':5}
>>> print(alien_0)
{'color': 'green', 'points': 5}
>>> alien_0['x_position'] = 0
>>> alien_0['y_position'] = 25
>>> print(alien_0)
{'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 25}
>>> alien_0 ={'color':'green','points':5}
>>> print(alien_0)
{'color': 'green', 'points': 5}
>>> del alien_0['points']
>>> print(alien_0)
{'color': 'green'}
使用字典来存储用户提供的数据或在编写能自动生成大量键—值对的代码时,通常需要先定义一个空字典
>>> alien_0 = {}
>>> alien_0['color'] = 'green'
>>> alien_0['points'] = 5
>>> print(alien_0)
{'color': 'green', 'points': 5}
alien_0 ={'color':'green'}
print("The alien is "+alien_0['color']+".")
alien_0['color'] = 'yellow'
print("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'}
alien_0['speed'] = 'fast'
print("Original x-position: " + str(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("New x-position: " + str(alien_0['x_position']))
Original x-position: 0
New x-position: 3
>>> favorite_languages = {
... 'jen':'python',
... 'sarah':'c',
... 'edward':'ruby',
... 'phil':'pyrhon',
... }
>>> print("Sarah's favorite language is "+
... favorite_languages['sarah'].title()+
... ".")
Sarah's favorite language is C.
>>> user_0 = { ... 'username':'efermi', ... 'first':'enrico', ... 'last':'fermi', ... } >>> for key ,value in user_0.items(): ... print("\nkey: "+key) ... print("Value: "+value) ... key: username Value: efermi key: first Value: enrico key: last Value: fermi
>>> favorite_languages = {
... 'jen':'python',
... 'sarah':'c',
... 'edward':'ruby',
... 'phil':'pyrhon',
... }
>>> for name,language in favorite_languages.items():
... print(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 Pyrhon.
for name in favorite_languages.keys():
for name in favorite_languages:
>>> favorite_languages = {
... 'jen': 'python',
... 'sarah': 'c',
... 'edward': 'ruby',
... 'phil': 'pyrhon',
... }
>>> for name in favorite_languages.keys():
... print(name.title())
...
Jen
Sarah
Edward
Phil
favorite_languages = { 'jen': 'python', 'sarah': 'c', 'edward': 'ruby', 'phil': 'pyrhon', } friends = ['phil','sarah'] 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() + "!") if 'erin' not in favorite_languages.keys(): print("Erin,please take our poll!")
Jen
Sarah
Hi Sarah, I see your favorite language is C!
Edward
Phil
Hi Phil, I see your favorite language is Pyrhon!
Erin,please take our 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
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
Python
Ruby
>>> user_0 = { ... 'username':'efermi', ... 'first':'enrico', ... 'last':'fermi', ... } >>> for key ,value in sorted(user_0.items()): ... print("\nkey: "+key) ... print("Value: "+value) ... key: first Value: enrico key: last Value: fermi key: username Value: efermi
>>> favorite_languages = {
... 'jen': 'python',
... 'sarah': 'c',
... 'edward': 'ruby',
... 'phil': 'pyrhon',
... }
>>> for name in sorted(favorite_languages.keys()):
... print(name.title())
...
Edward
Jen
Phil
Sarah
favorite_languages = {
'jen':'python',
'sarah':'c',
'edward':'ruby',
'phil':'python',
}
print("The following languages have been mentioned:")
for language in sorted(favorite_languages.values()):
print(language.title())
The following languages have been mentioned:
C
Python
Python
Ruby
favorite_languages = {
'jen':'python',
'sarah':'c',
'edward':'ruby',
'phil':'python',
}
print("The following languages have been mentioned:")
for language in sorted(set(favorite_languages.values())):
print(language.title())
The following languages have been mentioned:
C
Python
Ruby
>>> 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}
aliens = [] for alien_number in range(30): new_alien = {'color':'green','points':5,'speed':'slow'} aliens.append(new_alien) for alien in aliens[0:3]: if alien['color'] == 'green': alien['color'] = 'yellow' alien['speed'] = 'medium' alien['points'] = 10 elif alien['color'] == 'yellow': alien['color'] = 'red' alien['speed'] = 'fast' alien['points'] = 15 for alien in aliens[0:5]: print(alien) print("...")
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
...
pizza = {
'crust':'thick',
'toppings':['mushrooms','extra cheese'],
}
print("You ordered a " + pizza['crust'] + "-crust pizza" +
" with the following toppings:")
for topping in pizza['toppings']:
print("\t" + topping)
You ordered a thick-crust pizza with the following toppings:
mushrooms
extra cheese
>>> favorite_languages = { ... 'jen': ['python','ruby'], ... 'sarah': ['c'], ... 'edward': ['ruby','go'], ... 'phil':['python','haskell'], ... } >>> for name ,languages in favorite_languages.items(): ... if len(languages) == 1: ... for language in languages: ... print("\n" + name.title() + " 's favorite language is: " + language.title()) ... elif len(languages) > 1: ... print("\n" + name.title() + " 's favorite languages are:") ... for language in languages: ... print("\t" + language.title()) ... Jen 's favorite languages are: Python Ruby Sarah 's favorite language is: C Edward 's favorite languages are: Ruby Go Phil 's favorite languages are: Python Haskell
>>> users = { ... 'aeinstein':{ ... 'first':'albert', ... 'last':'einstein', ... 'location':'princeton' ... }, ... 'mcurie': { ... 'first': 'marie', ... 'last': 'curie', ... 'location': 'paris' ... }, ... } >>> for username,user_info in users.items(): ... print("\nUsername: " + username) ... full_name = user_info['first'] + " " + user_info['last'] ... location = user_info['location'] ... print("\tFull name: " + full_name.title()) ... print("\tLocation: " + location.title()) ... Username: aeinstein Full name: Albert Einstein Location: Princeton Username: mcurie Full name: Marie Curie Location: Paris
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。