随笔分类 - python
摘要:001、shell实现 [root@pc1 test2]# ls a.txt [root@pc1 test2]# cat a.txt ## 测试数据 a 8 b 3 a 9 a 2 b 3 c 7 c 2 c 10 [root@pc1 test2]# for i in a b c; do grep
阅读全文
摘要:001、 >>> list1 = [("aa",100, 400), ("bb", "kk", "yy"), (33, 400, 500)] >>> for i, j, k in list1: ## 利用列表、元组同时传入多个参数 ... print(i,k) ... aa 400 bb yy 33
阅读全文
摘要:001、 直接使用字典进行统计 >>> str1 = "aaaabbcdddefff" ## 测试字符串 >>> dict1 = dict() >>> for i in str1: ## 利用条件分支进行判断 ... if i in dict1: ... dict1[i] += 1 ... else
阅读全文
摘要:首先,defaultdict 是 dict 的一个子类。通常 Python 中字典(dict)是通过键值对来存取的,当索引一个不存在的键时,就会引发 keyerror 异常。那么,defaultdict 就可以解决这个问题,它可以实现为不存的键值返回一个默认值。 defaultdict是 colle
阅读全文
摘要:和map()类似,filter()也接收一个函数和一个序列。和map()不同的时,filter()把传入的函数依次作用于每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素。 >>> def is_odd(n): ... return n % 2 == 1 ... >>> filt
阅读全文
摘要:map函数会根据提供的函数对指定序列做映射。 通过定义可以看到,这个函数的第一个参数是一个函数,剩下的参数是一个或多个序列,返回值是一个集合。 map的作用是以参数序列中的每一个元素调用function函数,返回包含每次function函数返回值 001、 >>> map(lambda x: x *
阅读全文
摘要:#-*-coding: UTF-8 -*- 解决python2 中 中文乱码报错的问题 001、 [root@pc1 test1]# ls a.fa test.py [root@pc1 test1]# cat a.fa ## 测试数据 >chr1 xxx aaaggg eeee >chr2 yyy
阅读全文
摘要:001、 [root@pc1 test1]# ls a.fa test.py [root@pc1 test1]# cat a.fa ## 测试fasta文件 >chr1 kkk aattttt cccc >chr2 yyy ttttuuuu ddfff >chr3 eee uuuuukk sssff
阅读全文
摘要:001、 [root@pc1 test2]# ls a.fa test.py [root@pc1 test2]# cat a.fa ## 测试fasta文件 >chr1 aattggggc ssff xx >chr2 uuuttcccccc >chr3 ggggcccc tttt [root@pc1
阅读全文
摘要:Lambda函数用于创建匿名函数: 001、 >>> add1 = lambda x: x+2 >>> add1(100) 102 >>> add1(50) 52 >>> abc = lambda x: x * 2 >>> abc(10) 20 >>> abc(0.8) 1.6
阅读全文
摘要:001、 格式化数值 >>> "{:.2%}".format(0.123456789) '12.35%' >>> "{:.5%}".format(0.123456789) '12.34568%' >>> "{:.2}".format(0.123456789) '0.12' >>> "{:.5}".f
阅读全文
摘要:001、 any 只要一个结果为真,结果就为真 >>> any([False, False, True]) True >>> any([False, False, False]) False 002、all 只有所有结果为真才为真 >>> any([False, False, True]) True
阅读全文
摘要:001、 abs函数取绝对值 >>> abs(100) 100 >>> abs(-500) 500 002、round函数取小数点位数 >>> round(99.95) ## 默认没有小数点 100 >>> round(100.95) 101 >>> round(100.25) 100 >>> ro
阅读全文
摘要:eval()函数用来执行一个字符串表达式,并返回表达式的值。 001、 >>> a="3+3" >>> type(a) <class 'str'> >>> eval(a) ## eval用于返回字符串的值 6 >>> eval("pow(3,3)") 27 >>> eval("pow(2,3)")
阅读全文
摘要:001、 >>> import os >>> os.path.abspath("2.txt") ## 列出指定文件的绝对路径 '/home/test/2.txt' >>> os.path.abspath("test1") ## 列出指定目录的绝对路径 '/home/test/test1' 002、o
阅读全文
摘要:001、 >>> import os >>> os.listdir("/home/test") ## 结果返回的是列表 ['a.txt', 'b.txt', 'test1', 'test2', 'test3'] >>>
阅读全文
摘要:001、 [root@PC1 test]# ls a.txt test.py [root@PC1 test]# cat a.txt ## 测试文件 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019
阅读全文
摘要:001\ [root@pc1 test3]# cat a.fa >chr1 cccccggggggggttttgg cccgggggg >chr2 uuuuutttttNNNNNddffff dddgggggggggggcccccccccc [root@pc1 test3]# cat test.py
阅读全文
摘要:001、 [root@pc1 test3]# ls a.txt b.txt test.py [root@pc1 test3]# cat a.txt 1 2 3 [root@pc1 test3]# cat b.txt 1 2 3 [root@pc1 test3]# cat test.py ## 测试程
阅读全文
摘要:001、 [root@pc1 test3]# python2 ## python2 Python 2.7.5 (default, Jun 28 2022, 15:30:04) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2 Type "help",
阅读全文