# 快排:从小到大排# 思想:# 1.取中位数;# 2.以中位数为基准,将数组分为两半;# 3.分别对左右两半递归defquick_sort(list_):iflen(list_)<=1:return list_
midlle_num = list_[len(list_)//2]
right_list =[i for i in list_ if i > midlle_num]
midlle_list =[i for i in list_ if i == midlle_num]
left_list =[i for i in list_ if i < midlle_num]return quick_sort(left_list)+ midlle_list + quick_sort(right_list)if __name__ =='__main__':
a =[5,4,6,12,10,16]
b =sorted(a)print(b)
c = quick_sort(a)print(c)
dict_operations
# 1.更“保险地”由键获取值
d ={'cat':'cute','dog':'furry'}
d['fish']='wet'
c = d.get('fish','no')# 如果存在键'fish',则返回对应的值;如果不存在键'fish',则返回'no'。del d['fish']
e = d.get('fish','no')print(c)# wetprint(e)# no# 2.字典推导Dictionary comprehensions:和列表推导类似,但是允许你方便地构建字典。
nums =[0,1,2,3,4]
even_num_to_square ={x: x **2for x in nums if x %2==0}print(even_num_to_square)# Prints "{0: 0, 2: 4, 4: 16}"
string_operations
s ="hello"print(s.capitalize())# 首字母大写print(s.upper())# 全部统一为大写print(s.rjust(7))# 字符串右置,为凑够7个字符的长度,不够的以空格补充print(s.center(7))# 字符串居中,为凑够7个字符的长度,不够的以空格补充
import numpy as np
import matplotlib.pyplot as plt
# Compute the x and y coordinates for points on sine and cosine curves
x = np.arange(0,3* np.pi,0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)# Set up a subplot grid that has height 2 and width 1,# and set the first such subplot as active.
plt.subplot(2,1,1)# Make the first plot
plt.plot(x, y_sin)
plt.title('Sine')# Set the second subplot as active, and make the second plot.
plt.subplot(2,1,2)
plt.plot(x, y_cos)
plt.title('Cosine')# Show the figure.
plt.show()