美文网首页bioinfo
python实用函数整理

python实用函数整理

作者: jlyq617 | 来源:发表于2017-11-19 21:28 被阅读15次

1、小数的保留

print('%.nf'%number)#n代表保留小数的位数,number表示输入的数值

2、排列组合

package itertools

itertools具体介绍(含源代码):https://docs.python.org/2/library/itertools.html?highlight=itertools#itertools.combinations_with_replacement

permutations()
permutations(p[,r])#返回p中任意取r个元素做排列的元组的迭代器

#比如,从abc中取2个不同的字母的排列组合
itertools.permutations('abc',2)
eg.png
combinations()
combinations(iterable,r)#创建一个迭代器,返回iterable中所有长度为r的子序列,返回的子序列中的项按输入iterable中的顺序排序 note:不带重复
#比如,从abc中取2个字母的排列组合
itertools.combinations('abc',2)
eg2.png
combinations_with_replacement()
combinations_with_replacement(iterable,r)#与combinations不同在于该函数可以重复
#比如,从abc中取2个字母的排列组合
itertools.combinations_with_replacement('abc',2)
eg3.png
product()
product(iter1,iter2, ... iterN, [repeat=1]);创建一个迭代器,生成表示item1,item2等中的项目的笛卡尔积的元组,repeat是一个关键字参数,指定重复生成序列的次数
#比如,从abc中取2个的字母的所有排列组合
itertools.product('abc',repeat=2)
eg4.png

相关文章

网友评论

    本文标题:python实用函数整理

    本文链接:https://www.haomeiwen.com/subject/dqwpvxtx.html