我試圖找到2個字符串之間的所有插入形式。所以我有一個1400萬字符串的列表,然後我必須檢查每個字符串可能的插入可以將一個字符串轉換爲另一個字符串(基本上是計算插入頻率)。說x是一個字符串,y是另一個字符串,其中x是y的一個子字符串,因此我們必須找出將x轉換爲y的插入內容。在一個大列表中搜索子字符串
我正在使用以下代碼段。它可以工作,但正在走向很多時間。我甚至試圖在64個處理器上分配負載,但仍需要20天時間才能完成。
for i in Words:
#trying to distribute load across different processes, so can ignore this part
h = hashlib.sha256(i)
n = int(h.hexdigest(),base=16)
if (n%64!=ix): #ix is a process based id
continue
for j in Words:#
if len(i)>len(j):
continue
if(i!=j and i in j): # i is a substring of j
ind=j.find(i)
s1=j[0:ind]
s2=j[ind+len(i):len(j)]
if(len(s1)>0):
if (not transform.has_key(s1)):
transform[s1]=1
else:
transform[s1]+=1
if(len(s2)>0):
if (not transform.has_key(s2)):
transform[s2]=1
else:
transform[s2]+=1
使用PyPy嘗試。在大多數計算任務中,它比普通Python快得多。 – Blender
假設縮進是正確的。縮進在複製期間變得混亂。 – Slayer