python常用小功能

复制指定文件到目标目录

1
2
3
4
import shutil
source_path = '/source/file/full/path'
target_path = '/target/directory/full/path'
shutil.copy(source_path, target_path)

图片转换成Base64

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import base64


def pic_to_base64(pic_full_path):
with open(pic_full_path, "rb") as f:
# b64encode是编码,b64decode是解码
base64_data = base64.b64encode(f.read())
# base64.b64decode(base64data)

_base64_str = 'data:image/jpg;base64,%s' % base64_data.decode()
# print(_base64_str)
return _base64_str


if __name__ == '__main__':
base64_str = pic_to_base64('/your/picture/full/path')
print(base64_str)

从文件中读取地址,然后在浏览器中把地址标出来

  • 需要用到的文件有:

Numpy的基本使用

创建numpy数组

1
2
3
4
5
6
7
8
9
10
11
12
import numpy as np

# 三种方式创建numpy数组
n1 = np.array([1, 2, 3, 4, 5])
n2 = np.array(range(1, 6))
n3 = np.arange(1, 6)

print(type(n1)) # numpy.ndarray
print(n1.dtype) # int64

# 创建指定数据类型的numpy数组
n4 = np.array([1, 0, 0, 1, 0], dtype=bool)

numpy的索引和切片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import numpy as np

a = np.array(range(12)).reshape((3, 4))

# 取一行
print(a[2]) # array([ 8, 9, 10, 11])
print(a[2,:])
# 取连续的多行
print(a[1:])
print(a[1:,:])
# 取不连续的多行
print(a[[0,2]])
print(a[[0,2],:])

# 取一列
print(a[:,0]) # array([0, 4, 8])
# 取连续的多列
print(a[:,1:])
# 取不连续的多列
print(a[:,[0, 2]])

# 取多行和多列
# 取第二行第三列的值
print(a[1,2])
# 取第一行到第二行,第三列到第四列
print(a[0:2, 2:4])

# 取多个不相邻的点
# 取第一行第三列的点和第二行第四列的点
print(a[[0,1],[2,3]])

numpy数值的修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

import numpy as np

a = np.array(range(12)).reshape((3, 4))

a[2] = 9 # 找到索引或者切片后直接赋值就可以

# 将小于5的值替换为0
a[a < 5] = 0 # 会改变原来的numpy数组

# 将小于等于5的值替换为-5, 大于5的替换为10
b = np.where(a<=5, -5, 10) # 不会改变原来的numpy数组

# 把小于3的替换为3,大于9的替换为9
a.clip(3, 9)

数组的拼接

1
2
3
4
5
6
7
8
9
10
import numpy as np

t1 = np.array(range(12)).reshape((2, 6))
t2 = np.array(range(12, 24)).reshape((2, 6))

# 竖直拼接 (vsplit()方法竖直分割)
t3 = np.vstack((t1, t2))

# 水平拼接
t4 = np.hstack((t1,t2))

数组的行列交换

1
2
3
4
5
6
7
8
9
import numpy as np

t = np.array(range(12, 24)).reshape((3, 4))

# 行交换
t[[1,2],:] = t[[2,1],:] # 会改变原数组

# 列交换
t[:,[0,2]] = t[:,[2,0]]

numpy生成随机数

1
2
3
4
import numpy as np

np.random.randint(low=1,high=100,size=10,dtype=int)
np.random.rand(9)
常见排序算法的Python实现

冒泡排序

  冒泡排序重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。
  它的时间复杂度是O(N*N)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
data_set = [9, 1, 22, 31, 45, 3, 6, 2, 11]
data_len = len(data_set)
print('before sort: ', data_set)

loop_count = 0
for i in range(data_len):
for j in range(data_len - i - 1):
if data_set[j] > data_set[j+1]:
data_set[j], data_set[j+1] = data_set[j+1],data_set[j]
loop_count += 1
print(data_set)

# 排序后
print('after sort:', data_set)
print('loop count:', loop_count)

FastDFS的安装与使用

FastDFS的介绍

FastDFS只有两个角色:Tracker server和Storage server。Tracker server作为中心结点,其主要作用是负载均衡和调度。Tracker server在内存中记录分组和Storage server的状态等信息,不记录文件索引信息,占用的内存量很少。另外,客户端(应用)和Storage server访问Tracker server时,Tracker server扫描内存中的分组和Storage server信息,然后给出应答。

Introduction

The Introduction of this blog

这是通过Hexo搭建的个人博客,主要记录平时遇到的问题或者学习的记录

Hello World

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.