简体   繁体   English

如何在Python中提高此代码的效率?

[英]How to make this code more efficient in Python?

I'm having trouble running this nested for loop efficiently. 我在高效地运行此嵌套for循环时遇到麻烦。 I need to run this loops on a string s whose length is about 90,000. 我需要在长度约为90,000的字符串s上运行此循环。 Can anyone provide any tips? 谁能提供任何提示?

This code is supposed to take a string, and chop it up into pieces n sizes long such that the pieces are a continuous part of the original string. 该代码应采用一个字符串,并将其切成n个大小长的段,以使这些段成为原始字符串的连续部分。 The program then returns the size of each set for n up to the length of the string. 然后,程序将返回每个集合的大小n,直到字符串的长度。

For example: GATTACAT for n = 2 would produce {'GA', 'AT', 'TT', 'TA', 'AC', 'CA', 'AT' }. 例如:n = 2的GATTACAT将产生{'GA','AT','TT','TA','AC','CA','AT'}。 It would take the set of this so {'GA', 'AT', 'TT', 'TA', 'AC', 'CA'} and return its length. 它需要这样的集合,例如{'GA','AT','TT','TA','AC','CA'},并返回其长度。

The program is to do this from n = 0 to n = len('GATTACAT'), and sum all set lengths. 程序将从n = 0到n = len('GATTACAT')执行此操作,并对所有设置的长度求和。

for m in range(1, len(s)+1):
    sublist = list()
    for n in range(0, len(s)-m+1):
        sublist.append(''.join(ind[n:n+m]))
    sumS += len(set(sublist))

thanks! 谢谢!

Some quick ideas come to mind: 一些快速的想法浮现在脑海:

slen = 1 + len(s) # do this once, not a bunch of times in the loop
for m in range(1, slen):
    sublist = [''.join(ind[n:n+m]) for n in range(slen-m))] # list comps are usually faster than loops
    sumS += len(set(sublist))

Actually you can probably do it as a larger comprehension: 实际上,您可以将其理解为一个更大的理解:

slen = 1 + len(s)
sumS += sum(len(set(''.join(ind[n:n+m]) for n in range(slen-m))) for m in range(1,slen))

If you have Python 3 use a set comprehension instead of the list comprehension above. 如果您拥有Python 3,请使用set comprehension而不是上面的list comprehension。

>>> s = 'GATTACAT'

>>> [s[i:i+2] for i in range(len(s)-1)]
['GA', 'AT', 'TT', 'TA', 'AC', 'CA', 'AT']

>>> [s[i:i+3] for i in range(len(s)-2)]
['GAT', 'ATT', 'TTA', 'TAC', 'ACA', 'CAT']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM