0
我寫了下面的代碼來實現預期的結果。它的工作很好,直到200 input.i認爲優化這段代碼將解決我的問題。任何人都可以請幫我優化下面的代碼。任何人都可以幫助我優化我的代碼嗎?
from collections import defaultdict
number=input()
list_mine=[]
for i in range(1,number+1):
list_mine.append(raw_input(""))
#print list_mine
#understanding the number of unique occurence
unique=list(set(list_mine))
number_of_unique_occurence=len(unique)
d=defaultdict(int)
count=0
for i in unique:
for j in list_mine:
#print "i is "+str(i)
if i==j:
count = count+1
d[i]=count
count=0
print str(number_of_unique_occurence)
check = 0
counts =0
list_for=[]
for hel in list_mine:
#print "current element " + str(hel)
#print "index of that element is "+str(list_mine.index(hel))
check = check+1
if list_mine.index(hel) > 0:
#print "check = " +str(check)
for jj in range(0,list_mine.index(hel)):
#print jj
if hel == list_mine[jj]:
#print "elemnt are there"
break
else:
if counts == list_mine.index(hel)-1:
#print "greater then zero "+ str(hel) +" "+str(d[hel])
list_for.append(d[hel])
continue
counts=counts+1
else:
if check <= 1:
#print "at zero "+ str(d[hel])
list_for.append(d[hel])
print '%s' %' '.join(map(str, list_for))
樣品輸入
4
bcdef
abcdefg
bcde
bcdef
樣本輸出
3
2 1 1
「BCDEF」 出現兩次,在第一和最後位置輸入即,換言之出現一次每個。第一次出現的次序是「bcdef」,「abcdefg」和「bcde」因此是輸出。
我幾乎不敢問在200輸入時會發生什麼。 – TigerhawkT3
實際執行時間越來越多..注意到..我從來沒有優化過我的代碼。我認爲這將有助於解決問題嗎? –
我的代碼是否有阻塞元素增加執行時間? –