进阶第一步:Python编程 从入门到实践
你知道,就算大雨让这座城市颠倒,我也会……
接上次的第六章开始啦!
6.3 遍历字典
如果需要遍历字典中的所有键值对,可以使用方法items(),它返回一个键值对列表。
>>> use_0={
'username':'efermi',
'first':'enrico',
'last':'fermi',
}
>>> for key ,value in use_0.items():
print('\nKey: '+key)
print('Value: '+value)
Key: username
Value: efermi
Key: first
Value: enrico
Key: last
Value: fermi
>>> #再次印证了Python中的字典不关心键值对的存储顺序,而只追踪键和对之间的关联关系。
如果只需要遍历字典中的键,则可以使用方法keys();如果只要遍历字典中的值,则可以使用方法values()。
>>> friends=['phil','sarch']
>>> favourite_languages={
'jen':'python',
'sarah':'c',
'edward':'ruby',
'phil':'python',
}
>>> for name in favourite_languages.keys():
print(name.title())
if name in friends:
print('Hi '+name.title()+
',I see your favourite language is '+
favourite_languages[name].title()+'!')
Jen
Sarah
Edward
Phil
Hi Phil,I see your favourite language is Python!
>>> for name in sorted(favourite_languages.keys()):
print(name.title()+',thanks you for taking the poll.')
Edward,thanks you for taking the poll.
Jen,thanks you for taking the poll.
Phil,thanks you for taking the poll.
Sarah,thanks you for taking the poll.
>>> for value in favourite_languages.values():
print(value.title())
Python
C
Ruby
Python
>>> for value in set(favourite_languages.values()):
print(value.title())
C
Python
Ruby
>>> #set 可以让Python过滤掉重复的值,找出列表中独一无二的元素,并使用这些元素创建一个集合。
>>>
所谓嵌套,可以是一系列的字典存储到列表中,也可以是列表存储到字典中,还可以是字典存储到字典中。
学习到现在了,最痛苦的是什么呢?就是想了许多好玩的,看了许多好玩的,可是自己不会用啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊!!!
今天没时间看第七章的了,就这样吧!
If you are still looking for that one person who will change your life, take a look in the mirror!
先到这了,纯小白下次再来!
喜欢梅子酒,喜欢进步的你!
附:输错的代码,提醒自己!
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> use_0={
'username':'efermi',
'first':'enrico',
'last':'fermi',
}
>>> for key ,value in use_0.items():
print('\nKey: '+key)
print('Value: '+value)
Key: username
Value: efermi
Key: first
Value: enrico
Key: last
Value: fermi
>>>
>>> favourite_languages={
'jen':'python',
'sarah':'c',
'edward':'ruby',
'phil':'python',
}
>>> friends=['phil','sarch']
>>> for name in favourite_languages.key():
print(name.tilte())
if name in friends:
print('Hi '+name.title()+
',I see your favourite language is '+
favourite_languages[name].title()+'!')
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
for name in favourite_languages.key():
AttributeError: 'dict' object has no attribute 'key'
>>> friends=['phil','sarch']
>>> for name in favourite_languages.keys():
print(name.tilte())
if name in friends:
print('Hi '+name.title()+
',I see your favourite language is '+
favourite_languages[name].title()+'!')
SyntaxError: multiple statements found while compiling a single statement
>>> favourite_languages={
'jen':'python',
'sarah':'c',
'edward':'ruby',
'phil':'python',
}
>>> friends=['phil','sarch']
>>> for name in favourite_languages.keys():
print(name.tilte())
if name in friends:
print('Hi '+name.title()+
',I see your favourite language is '+
favourite_languages[name].title()+'!')
SyntaxError: multiple statements found while compiling a single statement
>>>
>>> friends=['phil','sarch']
>>> favourite_languages={
'jen':'python',
'sarah':'c',
'edward':'ruby',
'phil':'python',
}
>>> for name in favourite_languages.keys():
print(name.tilte())
if name in friends:
print('Hi '+name.title()+
',I see your favourite language is '+
favourite_languages[name].title()+'!')
Traceback (most recent call last):
File "<pyshell#32>", line 2, in <module>
print(name.tilte())
AttributeError: 'str' object has no attribute 'tilte'
>>> friends=['phil','sarch']
>>> favourite_languages={
'jen':'python',
'sarah':'c',
'edward':'ruby',
'phil':'python',
}
SyntaxError: multiple statements found while compiling a single statement
>>> friends=['phil','sarch']
SyntaxError: unexpected indent
>>> friends=['phil','sarch']
>>> favourite_languages={
'jen':'python',
'sarah':'c',
'edward':'ruby',
'phil':'python',
}
>>> for name in favourite_languages.keys():
print(name.title())
if name in friends:
print('Hi '+name.title()+
',I see your favourite language is '+
favourite_languages[name].title()+'!')
Jen
Sarah
Edward
Phil
Hi Phil,I see your favourite language is Python!
>>> friends=['phil','sarch']
>>> favourite_languages={
'jen':'python',
'sarah':'c',
'edward':'ruby',
'phil':'python',
}
SyntaxError: multiple statements found while compiling a single statement
>>> friends=['phil','sarch']
>>> favourite_languages={
'jen':'python',
'sarah':'c',
'edward':'ruby',
'phil':'python',
}
SyntaxError: unexpected indent
>>> favourite_languages={
'jen':'python',
'sarah':'c',
'edward':'ruby',
'phil':'python',
}
>>> for name in favourite_languages.keys():
print(name.title())
Jen
Sarah
Edward
Phil
>>> for name in favourite_languages.keys():
print(name.title())
if name in friends:
print('Hi '+name.title()+
',I see your favourite language is '+
favourite_languages[name].title()+'!')
SyntaxError: invalid syntax
>>> for name in favourite_languages.keys():
print(name.title())
Jen
Sarah
Edward
Phil
>>> favourite_languages
{'jen': 'python', 'sarah': 'c', 'edward': 'ruby', 'phil': 'python'}
>>> for name in sorted(language_language.keys()):
print(name.title()+',thanks you for taking the poll.')
Traceback (most recent call last):
File "<pyshell#54>", line 1, in <module>
for name in sorted(language_language.keys()):
NameError: name 'language_language' is not defined
>>> for name in sorted(favourite_language.keys()):
print(name.title()+',thanks you for taking the poll.')
Traceback (most recent call last):
File "<pyshell#56>", line 1, in <module>
for name in sorted(favourite_language.keys()):
NameError: name 'favourite_language' is not defined
>>> for name in sorted(favourite_languages.keys()):
print(name.title()+',thanks you for taking the poll.')
Edward,thanks you for taking the poll.
Jen,thanks you for taking the poll.
Phil,thanks you for taking the poll.
Sarah,thanks you for taking the poll.
>>> for value in favourite_languages.values():
print(value.title())
Python
C
Ruby
Python
>>> for value in set(favourite_languages.values()):
print(value.title())
C
Python
Ruby
>>> #set 可以让Python过滤掉重复的值,找出列表中独一无二的元素,并使用这些元素创建一个集合。
>>>
RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36-32/天气.py
Traceback (most recent call last):
File "C:/Users/Administrator/AppData/Local/Programs/Python/Python36-32/天气.py", line 2, in <module>
import winshell
ModuleNotFoundError: No module named 'winshell'
>>>