0%

列表迭代

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

安装Hexo

安装完Git和Node.js,开始安装Hexo,安装Hexo需要翻墙,打开终端,输入以下命令:

1
$ sudo npm install -g hexo-cli

新建目录存放博客的网站文件,例如新建一个空文件夹Blog,然后执行以下命令

1
2
3
$ hexo init Blog
$ cd Blog
$ npm Blog

发布新的文章

创建完网站目录后,开始写作,创建一篇新的文章,执行以下命令

1
$ hexo new "HelloWord"

执行完上述命令,会在网站的~/Blog/source/_posts目录下出现HelloWord.md文件,这个文件就是我们新建的文章,使用Markdown语法编写我们的文章。

生成静态文件

文章写好之后,执行以下命令,开始生成静态文件:

1
$ hexo g

启动服务器

静态文件生成之后,启动本地服务器,预览文章的总体页面,需要执行以下命令:

1
$ hexo s

本地浏览器预览效果:

发布到Github

修改配置文件

在配置文件中配置发布到git,然后填写具体的仓库地址:

1
2
3
4
## Docs: https://hexo.io/docs/deployment.html
deploy:
type: git
repo: https://github.com/DavidWanderer/Blog.git

安装Hexo发布到git的插件:

1
$ npm install hexo-deployer-git --save

检查插件是否安装完成:

1
$ npm list hexo-deployer-git

将本地的静态文件部署到github的仓库

执行命令,首次执行命令的时候会让你输入github的用户名和密码:

1
$ hexo d

最后访问giuhub的地址效果:

参考链接

Hexo官网
Hexo + Github 搭建个人博客