Python使用技巧
VS Code 相关技巧
缩进快捷键
向左缩进:
Ctrl + [
向右缩进:
Ctrl + ]
同时编辑多行
Alt + Shift
: 竖列选择这种模式下只可以选择竖列,不可以随意插入光标。所以只限制于同一列且不间隔的情况下。Shift + Ctrl
: 竖列选择Ctrl+Click
,选择多个编辑位点。这种模式下不仅可以选择竖列,同时还可以在多个地方插入光标。
Python更换pip源(pypi镜像) - 清华大学开源软件镜像站
临时使用(注意,simple 不能少, 是 https 而不是 http)
设为默认(升级 pip 到最新版本后进行配置)
如果您到 pip 默认源的网络连接较差,可临时使用本镜像站来升级 pip
Python计算排列数与组合数
使用第三方模块scipy计算排列组合的具体数值
使用阶乘的方式求组合数
使用itertools列出排列组合的全部情况
附:MATLAB计算排列组合数
求n的阶乘
求组合数
求排列数
累积求积函数cumprod()
使用
format rat
命令即可使输出结果转化为分数形式
Python中if __name__ == '__main__':
的作用和原理
if __name__ == '__main__':
的作用和原理作用
一个python文件通常有两种使用方法,第一是作为脚本直接执行,第二是 import
到其他的 python 脚本中被调用(模块重用)执行。因此 if __name__ == 'main':
的作用就是控制这两种情况执行代码的过程,在 if __name__ == 'main':
下的代码只有在第一种情况下(即文件作为脚本直接执行)才会被执行,而 import 到其他脚本中是不会被执行的。举例说明:新建 test.py
,内容如下:
直接执行 test.py
,结果如下,可见 if __name__=="__main__":
语句之前和之后的代码都被执行。
下面尝试 import
执行。在同一文件夹新建名称为 import-test.py
的脚本,内容如下。执行之,结果仅为this is one
。
原理
每个python模块(python文件,也就是此处的 test.py 和 import-test.py)都包含内置的变量 __name__
,当该模块被直接执行的时候,__name__
等于文件名(包含后缀.py
);如果该模块 import
到其他模块中,则该模块的 __name__
等于模块名称(不包含后缀.py
)。 而 __main__
始终指当前执行模块的名称(包含后缀.py
)。进而当模块被直接执行时,__name__ == '__main__'
结果为真。
为了进一步说明,我们在 test.py
脚本的 if __name__=="__main__":
之前加入 print(__name__)
,即将 __name__
打印出来。结果如下:直接执行时输出为__main__
,import
执行时输出为test
。
修改Python IDLE初始默认文件打开/保存路径的方法
找到桌面或者开始菜单里的Python IDLE快捷方式,或者直接打开安装目录下的pythonw.exe。右击之,选择“属性”,在属性窗口中可对“起始位置”进行修改,即可更改默认文件打开/保存路径。
Biopython教程与手册
Python教程系列-王的机器
Python基础教程-C语言中文网
print()
函数详细语法: print(value, ..., sep='', end='\n', file=sys.stdout, flush=False)
Python中统计列表元素的出现次数并降序排序
Python有序字典(OrderedDict)与普通字典(dict)
无序字典(普通字典)
输出:
可以看见,遍历一个普通字典,返回的数据和定义字典时的字段顺序是不一致的。注意: Python3.6改写了dict的内部算法,Python3.6版本以后的dict是有序的,所以也就无须再关注dict顺序性的问题
有序字典
输出:
有序字典可以按字典中元素的插入顺序来输出。注意: 有序字典的作用只是记住元素插入顺序并按顺序输出。如果有序字典中的元素一开始就定义好了,后面没有插入元素这一动作,那么遍历有序字典,其输出结果仍然是无序的,因为缺少了有序插入这一条件,所以此时有序字典就失去了作用,所以有序字典一般用于动态添加并需要按添加顺序输出的时候。
Python gzip
模块
gzip
模块Python gzip module provides a very simple way to compress and decompress files and work in a similar manner to GNU programs gzip and gunzip.
编写压缩文件
读取压缩文件
17 Statistical Hypothesis Tests in Python
https://machinelearningmastery.com/statistical-hypothesis-tests-in-python-cheat-sheet/ By Jason Brownlee on August 15, 2018 in Statistics
Normality Tests
This section lists statistical tests that you can use to check if your data has a Gaussian distribution.
Shapiro-Wilk Test
Tests whether a data sample has a Gaussian distribution.
Assumptions
Observations in each sample are independent and identically distributed (iid).
Interpretation
H0: the sample has a Gaussian distribution.
H1: the sample does not have a Gaussian distribution.
Python Code
More Information
D’Agostino’s K^2 Test
Tests whether a data sample has a Gaussian distribution.
Assumptions
Observations in each sample are independent and identically distributed (iid).
Interpretation
H0: the sample has a Gaussian distribution.
H1: the sample does not have a Gaussian distribution.
Python Code
More Information
Anderson-Darling Test
Tests whether a data sample has a Gaussian distribution.
Assumptions
Observations in each sample are independent and identically distributed (iid).
Interpretation
H0: the sample has a Gaussian distribution.
H1: the sample does not have a Gaussian distribution.
Python Code
More Information
Correlation Tests
This section lists statistical tests that you can use to check if two samples are related.
Pearson’s Correlation Coefficient
Tests whether two samples have a linear relationship.
Assumptions
Observations in each sample are independent and identically distributed (iid).
Observations in each sample are normally distributed.
Observations in each sample have the same variance.
Interpretation
H0: the two samples are independent.
H1: there is a dependency between the samples.
Python Code
More Information
Spearman’s Rank Correlation
Tests whether two samples have a monotonic relationship.
Assumptions
Observations in each sample are independent and identically distributed (iid).
Observations in each sample can be ranked.
Interpretation
H0: the two samples are independent.
H1: there is a dependency between the samples.
Python Code
More Information
Kendall’s Rank Correlation
Tests whether two samples have a monotonic relationship.
Assumptions
Observations in each sample are independent and identically distributed (iid).
Observations in each sample can be ranked.
Interpretation
H0: the two samples are independent.
H1: there is a dependency between the samples.
Python Code
More Information
Chi-Squared Test
Tests whether two categorical variables are related or independent.
Assumptions
Observations used in the calculation of the contingency table are independent.
25 or more examples in each cell of the contingency table.
Interpretation
H0: the two samples are independent.
H1: there is a dependency between the samples.
Python Code
More Information
Stationary Tests
This section lists statistical tests that you can use to check if a time series is stationary or not.
Augmented Dickey-Fuller Unit Root Test
Tests whether a time series has a unit root, e.g. has a trend or more generally is autoregressive.
Assumptions
Observations in are temporally ordered.
Interpretation
H0: a unit root is present (series is non-stationary).
H1: a unit root is not present (series is stationary).
Python Code
More Information
Kwiatkowski-Phillips-Schmidt-Shin
Tests whether a time series is trend stationary or not.
Assumptions
Observations in are temporally ordered.
Interpretation
H0: the time series is trend-stationary.
H1: the time series is not trend-stationary.
Python Code
More Information
Parametric Statistical Hypothesis Tests
This section lists statistical tests that you can use to compare data samples.
Student’s t-test
Tests whether the means of two independent samples are significantly different.
Assumptions
Observations in each sample are independent and identically distributed (iid).
Observations in each sample are normally distributed.
Observations in each sample have the same variance.
Interpretation
H0: the means of the samples are equal.
H1: the means of the samples are unequal.
Python Code
More Information
Paired Student’s t-test
Tests whether the means of two paired samples are significantly different.
Assumptions
Observations in each sample are independent and identically distributed (iid).
Observations in each sample are normally distributed.
Observations in each sample have the same variance.
Observations across each sample are paired.
Interpretation
H0: the means of the samples are equal.
H1: the means of the samples are unequal.
Python Code
More Information
Analysis of Variance Test (ANOVA)
Tests whether the means of two or more independent samples are significantly different.
Assumptions
Observations in each sample are independent and identically distributed (iid).
Observations in each sample are normally distributed.
Observations in each sample have the same variance.
Interpretation
H0: the means of the samples are equal.
H1: one or more of the means of the samples are unequal.
Python Code
More Information
Repeated Measures ANOVA Test
Tests whether the means of two or more paired samples are significantly different.
Assumptions
Observations in each sample are independent and identically distributed (iid).
Observations in each sample are normally distributed.
Observations in each sample have the same variance.
Observations across each sample are paired.
Interpretation
H0: the means of the samples are equal.
H1: one or more of the means of the samples are unequal.
Python Code
Currently not supported in Python.
More Information
Nonparametric Statistical Hypothesis Tests
Mann-Whitney U Test
Tests whether the distributions of two independent samples are equal or not.
Assumptions
Observations in each sample are independent and identically distributed (iid).
Observations in each sample can be ranked.
Interpretation
H0: the distributions of both samples are equal.
H1: the distributions of both samples are not equal.
Python Code
More Information
Wilcoxon Signed-Rank Test
Tests whether the distributions of two paired samples are equal or not.
Assumptions
Observations in each sample are independent and identically distributed (iid).
Observations in each sample can be ranked.
Observations across each sample are paired.
Interpretation
H0: the distributions of both samples are equal.
H1: the distributions of both samples are not equal.
Python Code
More Information
Kruskal-Wallis H Test
Tests whether the distributions of two or more independent samples are equal or not.
Assumptions
Observations in each sample are independent and identically distributed (iid).
Observations in each sample can be ranked.
Interpretation
H0: the distributions of all samples are equal.
H1: the distributions of one or more samples are not equal.
Python Code
More Information
Friedman Test
Tests whether the distributions of two or more paired samples are equal or not.
Assumptions
Observations in each sample are independent and identically distributed (iid).
Observations in each sample can be ranked.
Observations across each sample are paired.
Interpretation
H0: the distributions of all samples are equal.
H1: the distributions of one or more samples are not equal.
Python Code
More Information
Further Reading
This section provides more resources on the topic if you are looking to go deeper.
Python循环删除列表元素常见错误与正确方法
https://blog.csdn.net/u013555719/article/details/84550700
常见错误一:使用固定长度循环删除列表元素
原因是在删除list中的元素后,list的实际长度变小了,但是循环次数没有减少,依然按照原来list的长度进行遍历,所以会造成索引溢出
常见错误二:正序循环遍历删除列表元素
不能删除连续的情况
当符合条件,删除元素[2]之后,后面的元素全部往前移,但是索引并不会随着值向前移动而变化,而是接着上一个位置向后移动。这样就会漏掉解
正确的方法一:倒序循环遍历
正确的方法二:遍历拷贝的list,操作原始的list
原始的list是num_list,那么其实,num_list[:]是对原始的num_list的一个拷贝,是一个新的list,所以,我们遍历新的list,而删除原始的list中的元素,则既不会引起索引溢出,最后又能够得到想要的最终结果。此方法的缺点可能是,对于过大的list,拷贝后可能很占内存。那么对于这种情况,可以用倒序遍历的方法来实现。
Python汉字拼音转换工具——pypinyin
pypinyin
https://zhuanlan.zhihu.com/p/374674547?utm_id=0
安装
基本使用
使用命令行一键识别拼音:
高级使用
详见参考链接
Last updated