亳州建设网站公司,包装设计接单网站,做app模板网站有哪些内容,大连h5网站建设上一篇为啥我的Python这么慢, 字符串的加和和join被陈群主分享到biopython-生信QQ群时#xff0c;乐平指出字典的写法存在问题#xff0c;并给了一篇知乎的链接https://zhuanlan.zhihu.com/p/28738634指导如何高效字典操作。
根据那篇文章改了两处写法#xff0c;如下 (存储…上一篇为啥我的Python这么慢, 字符串的加和和join被陈群主分享到biopython-生信QQ群时乐平指出字典的写法存在问题并给了一篇知乎的链接https://zhuanlan.zhihu.com/p/28738634指导如何高效字典操作。
根据那篇文章改了两处写法如下 (存储于readFaJoin2.py文件中)
from collections import defaultdictaDict defaultdict(list)for line in open(GRCh38.fa):if line[0] :key line[1:-1]else:aDict[key].append(line.strip())
#----------------------------------------
for key, value in aDict.iteritems():aDict[key] .join(value)
比之前提速接近2s。一个是使用了defaultdict初始化字典另外一个是用iteritems遍历字典节省近一半的内存。
time python readFaJoin2.pyreal 0m49.114s
user 0m38.442s
sys 0m10.565s
defaultdict用在这效果不太明显之前处理全基因组每个位点数据的频繁存取时defaultdict在程序无论速度还是写法上都有很大提升。
字典本身还有更多高效用法可以去参考知乎的那篇文章。这儿介绍的是妙用字典的哈希属性快速查找项。
在生信操作中常常会在一个大矩阵中匹配已小部分基因或位点提取关注的基因或位点的信息。最开始的写法是
targetL [a, n, c, d]
if item in targetL:other_operations
后来随着数据量变大发现这个速度并不快于是换了下面的方式
targetL [a, n, c, d]
targetD dict.fromkeys(targetL, 0)if item in targetD:other_operations
又可以愉快的查询了。
为什么呢
这是因为在Pyhton中列表的查询时间复杂度是O(n)(n是列表长度)字典的查询负责度是O(1)(与字典长度无关)。
字典的查询复杂度为什么是O(1)呢 Python中实现了一个hash函数把字典的key转换为哈希值组成连续地址的数字哈希表。字典的每次查询转换为了从数组特定位置取出一个元素所以时间复杂度为O(1)。
后来发现python中set也是用hash table存储所以上面的程序可以更简化而不影响速度。
targetS set([a, n, c, d])if item in targetS:other_operations
那么速度到底差多大有没有直观一些的展示呢? 这是StackOverflow的一个简化例子, 百万倍速度差异。
ctehbio:~$ python -mtimeit -s drange(10**7) 5*10**6 in d
10 loops, best of 3: 182 msec per loop
ctehbio:~$ python -mtimeit -s ddict.fromkeys(range(10**7)) 5*10**6 in d
10000000 loops, best of 3: 0.16 usec per loop
ctehbio:~$ python -mtimeit -s dset(range(10**7)) 5*10**6 in d
10000000 loops, best of 3: 0.164 usec per loop
Ref: 速度测试例子 https://stackoverflow.com/questions/513882/python-list-vs-dict-for-look-up-table python各数据结构时间复杂度 https://wiki.python.org/moin/TimeComplexity