優化此合併排序功能的任何想法?輸入是這樣一個列表:[[(1,'i'),(3,'i'),(5,'i'),(8,'i')],[(2 ,'(','')],[(4,'t'),(7,'t')],[(6,'a')],[ E')],並且輸出就是兩個字:‘主動性’在Python中優化合並排序功能
def merge(decks):
while len(decks) > 1:
del1 = decks.pop(0)
del2 = decks.pop(0)
total = list()
while (len(del1) and len(del2)) > 0:
if del1[0] < del2[0]:
total.append(del1.pop(0))
else:
total.append(del2.pop(0))
total.extend(del1)
total.extend(del2)
decks.append(total)
word = ""
for kort in decks[0]:
word += kort[1]
return word
'(LEN(DEL1)和len(DEL2))> 0'僥倖得來的工作BTW :) –
你可以進行一些小的優化,但是這更多的是對話題對於http://代碼審查。 stackexchange.com –
@ Jean-FrançoisFabre你是什麼意思? –