0%

Python学习笔记(使用for迭代)

列表迭代

1
2
3
rabbits = ['Flopsy', 'Mopsy', 'Cottontail', 'Peter']
for rabbit in rabbits:
print(rabbit)

运行结果:

1
2
3
4
Flopsy
Mopsy
Cottontail
Peter

字符串迭代

1
2
3
word = 'cat'
for letter in word:
print(letter)

运行结果:

1
2
3
c
a
t

字典的迭代

1
2
3
4
5
6
7
8
9
10
11
accusation = {'room':'ballroom', 'weapon':'lead pipe', 'person':'Col. Mustard'}
# 对字典的Key进行迭代
for card in accusation:
print(card)
for card in accusation.keys():
print(card)
# 对字典的Value进行迭代
for item in accusation.items():
print(item)
for card, contents in accusation.items():
print('Card', card, 'has the contents', contents)

运行结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
room
weapon
person
room
weapon
person
ballroom
lead pipe
Col. Mustard
('room', 'ballroom')
('weapon', 'lead pipe')
('person', 'Col. Mustard')
Card room has the contents ballroom
Card weapon has the contents lead pipe
Card person has the contents Col. Mustard

for … else … 判断迭代是否正常结束,如果正常结束,执行else代码块,如果是break非正常结束,不执行else代码块

1
2
3
4
5
6
7
8
9
cheeses = ['aaa', 'bb', 'cc']
for cheese in cheeses:
if cheese == 'aa':
print('找到目标break跳出循环')
break
else:
print('没找到')
else:
print('没有找到目标,执行了else的模块')

运行结果:

1
2
3
4
没找到
没找到
没找到
没有找到目标,执行了else的模块

使用zip()进行并行迭代

1
2
3
4
5
6
7
days = ['Monday', 'Tuesday', 'Wednesday']
fruits = ['banana', 'orange', 'peach']
drinks = ['coffee', 'tea', 'beer']
desserts = ['tiramiu', 'ice cream', 'pie', 'pudding']

for day, fruit, drink, dessert in zip(days, fruits, drinks, desserts):
print(day, ": drink", drink, "- eat", fruit, "- enjoy", dessert)

运行结果:

1
2
3
Card room has the contents ballroom
Card weapon has the contents lead pipe
Card person has the contents Col. Mustard

使用dict() 和 zip()创建微型字典

1
2
3
4
english = 'Monday', 'Tuesday', 'Wednesday'
french = 'Lundi', 'Mardi', 'Mercredi'
print(list(zip(english, french)))
print(dict(zip(english, french)))

运行结果:

1
2
[('Monday', 'Lundi'), ('Tuesday', 'Mardi'), ('Wednesday', 'Mercredi')]
{'Monday': 'Lundi', 'Tuesday': 'Mardi', 'Wednesday': 'Mercredi'}

使用range()生成自然数序列

1
2
3
for x in range(0, 3):
print(x)
print(list(range(0, 3)))

运行结果:

1
2
3
4
0
1
2
[0, 1, 2]

使用range()反向创建序列

1
2
3
for x in range(2, -1, -1):
print(x)
print(list(range(2, -1, -1)))

运行结果:

1
2
3
4
2
1
0
[2, 1, 0]

获取从0到10的偶数

1
print(list(range(0, 11, 2))

运行结果:

1
[0, 2, 4, 6, 8, 10]

列表推导式

1
2
3
4
5
number_list = [number for number in range(1, 6)]
print(number_list)

number_list = [number-1 for number in range(11, 16)]
print(number_list)

运行结果:

1
2
[1, 2, 3, 4, 5]
[10, 11, 12, 13, 14]

创建1-5之间的奇数列表

1
2
a_list = [number for number in range(1, 6) if number % 2 == 1]
print(a_list)

运行结果:

1
[1, 3, 5]

创建1-5之间的偶数列表

1
2
a_list = [number for number in range(1, 6) if number % 2 == 0]
print(a_list)

运行结果:

1
[2, 4]

多个for循环推导模拟嵌套循环

1
2
3
4
5
rows = range(1, 4)
cols = range(1, 3)
cells = [(row, col) for row in rows for col in cols]
for cell in cells:
print(cell)

运行结果:

1
2
3
4
5
6
(1, 1)
(1, 2)
(2, 1)
(2, 2)
(3, 1)
(3, 2)

字典推导式

1
2
3
4
5
6
7
word = 'letters'
# 迭代单词中包含的字母,并计算该字母在单词中出现的次数
letter_counts = {letter: word.count(letter) for letter in word}
print(letter_counts)

letter_counts = {letter: word.count(letter) for letter in word}
print(letter_counts)

运行结果:

1
2
{'l': 1, 'e': 2, 't': 2, 'r': 1, 's': 1}
{'s': 1, 'l': 1, 't': 2, 'e': 2, 'r': 1}

集合推导式

1
2
a_set = {number for number in range(1, 6) if number % 3 == 1}
print(a_set)

运行结果:

1
{1, 4}

生成器推导式

1
2
3
4
5
6
7
8
9
number_thing = (number for number in range(1, 6))
# 返回的是一个生成器对象
print(type(number_thing))
# 方式1:直接对生成器对象进行迭代,注意生成器对象只能被迭代一次,迭代完成之后被擦除
for number in number_thing:
print(number)
# 方式2:将生成器对象转换成列表,然后再进行迭代
# number_list = list(number_thing)
# print(number_list)

运行结果:

1
2
3
4
5
6
<class 'generator'>
1
2
3
4
5