This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""最小公倍数与最大公约数""" | |
def gcd(*numbers): | |
"""Return the greatest common divisor of the given integers""" | |
from fractions import gcd | |
return reduce(gcd, numbers) | |
def lcm(*numbers): | |
"""Return lowest common multiple.""" | |
def lcm(a, b): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!pip install functools | |
from functools import partial | |
import numpy | |
from matplotlib import pyplot | |
# Define a PDF | |
x_samples = numpy.arange(-3, 3.01, 0.01) | |
PDF = numpy.empty(x_samples.shape) | |
PDF[x_samples < 0] = numpy.round(x_samples[x_samples < 0] + 3.5) / 3 | |
PDF[x_samples >= 0] = 0.5 * numpy.cos(numpy.pi * x_samples[x_samples >= 0]) + 0.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Python并没有switch-case的语法,等效的用法要么是像上面一样用if-elif-else的组合,要么可以考虑字典""" | |
pets = ['dog', 'cat', 'droid', 'fly'] | |
food_for_pet = { | |
'dog': 'steak', | |
'cat': 'milk', | |
'droid': 'oil', | |
'fly': 'sh*t' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
moves = ['up', 'left', 'down', 'right'] | |
def move_up(x): # 定义向上的操作 | |
x[1] += 1 | |
def move_down(x): # 定义向下的操作 | |
x[1] -= 1 | |
def move_left(x): # 定义向左的操作 | |
x[0] -= 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""对于filter和map,在Python2中返回结果是列表,Python3中是生成器""" | |
import functools | |
map(lambda x: x**2, [1, 2, 3, 4]) # [1, 4, 9, 16] | |
map(lambda x, y: x + y, [1, 2, 3], [5, 6, 7]) # [6, 8, 10] | |
functools.reduce(lambda x, y: x + y, [1, 2, 3, 4]) # ((1+2)+3)+4=10 | |
filter(lambda x: x % 2, [1, 2, 3, 4, 5]) # 筛选奇数,[1, 3, 5] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = 'Life is short, you need Python' | |
a.lower() # 'life is short, you need Python' | |
a.upper() # 'LIFE IS SHORT, YOU NEED PYTHON' | |
a.count('i') # 2 | |
a.find('e') # 从左向右查找'e',3 | |
a.rfind('need') # 从右向左查找'need',19 | |
a.replace('you', 'I') # 'Life is short, I need Python' | |
tokens = a.split() # ['Life', 'is', 'short,', 'you', 'need', 'Python'] | |
b = ' '.join(tokens) # 用指定分隔符按顺序把字符串列表组合成新字符串 | |
c = a + '\n' # 加了换行符,注意+用法是字符串作为序列的用法 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = 'I’m like a {} chasing {}.' | |
# 按顺序格式化字符串,'I’m like a dog chasing cars.' | |
a.format('dog', 'cars') | |
# 在大括号中指定参数所在位置 | |
b = 'I prefer {1} {0} to {2} {0}' | |
b.format('food', 'Chinese', 'American') | |
# >代表右对齐,>前是要填充的字符,依次输出: | |
# 000001 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with open('name_age.txt', 'r') as fread, open('age_name.txt', 'w') as fwrite: | |
line = fread.readline() | |
while line: | |
name, age = line.rstrip().split(',') | |
fwrite.write('{},{}\n'.format(age, name)) | |
line = fread.readline() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for filepath in filelist: # filelist中是文件路径的列表 | |
try: | |
with open(filepath, 'r') as f: | |
# 执行数据处理的相关工作 | |
pass | |
print('{} is processed!'.format(filepath)) | |
except IOError: | |
print('{} with IOError!'.format(filepath)) | |
# 异常的相应处理 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Target: | |
1、第1章 第1节 集合(1)2、第1章 第1节 集合(2)3、第1章 第1节 集合 | |
Purpose: | |
Search for "1、" and replace with newline plus that | |
Solution: | |
Search: (\d{1,3}\、) | |
Replace: \n\1 | |
----------------------------------------------------------------------- | |
Target: | |
第14章 第2节 第二类曲线积分与第二类曲面积分 |
OlderNewer