Skip to content

Instantly share code, notes, and snippets.

View lhsfcboy's full-sized avatar
🌴
On vacation

Mike Hongshuai Luo lhsfcboy

🌴
On vacation
  • Feicheng,China
View GitHub Profile
@lhsfcboy
lhsfcboy / gcd_and_lcm.py
Last active June 26, 2017 01:54
GCD and LCM functions in Python
"""最小公倍数与最大公约数"""
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):
@lhsfcboy
lhsfcboy / simulte_unknown_distribution.py
Created June 26, 2017 01:57
模拟未知的分布并绘图展示
#!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
@lhsfcboy
lhsfcboy / switch_case.py
Created June 26, 2017 05:31
Python中模拟switch-case语句
"""Python并没有switch-case的语法,等效的用法要么是像上面一样用if-elif-else的组合,要么可以考虑字典"""
pets = ['dog', 'cat', 'droid', 'fly']
food_for_pet = {
'dog': 'steak',
'cat': 'milk',
'droid': 'oil',
'fly': 'sh*t'
}
@lhsfcboy
lhsfcboy / function_as_key.py
Created June 26, 2017 05:40
把函数作为字典的值
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
@lhsfcboy
lhsfcboy / map_reduce_filter_python3.py
Last active June 26, 2017 06:22
Python中的函数编程
"""对于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]
@lhsfcboy
lhsfcboy / string_handle.py
Created June 26, 2017 06:32
Python中字符串相关的处理
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' # 加了换行符,注意+用法是字符串作为序列的用法
@lhsfcboy
lhsfcboy / string_format.py
Created June 26, 2017 06:33
Python使用format格式化字符串
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
@lhsfcboy
lhsfcboy / open_two_files.py
Created June 26, 2017 06:35
同时打开两个文件
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()
@lhsfcboy
lhsfcboy / try_catch_python_example.py
Last active August 4, 2017 04:06
try_catch示例
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))
# 异常的相应处理
@lhsfcboy
lhsfcboy / regular_expression.txt
Last active November 6, 2017 04:36
notepad_plus_plus中的正则表达式
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节 第二类曲线积分与第二类曲面积分