0
我試圖實現合併排序在Python蟒蛇歸併排序實現結果在列表嵌套
#!/usr/bin/python
import math
def merge_sort(unsorted):
if(len(unsorted)<2):
return unsorted
middle = int(math.floor(len(unsorted)/2))
left = merge_sort(unsorted[0:middle])
right = merge_sort(unsorted[middle:])
return merge(left, right)
def merge(left, right):
results = []
while (len(left) > 0 and len(right) > 0):
if(left[0] < right[0]):
results.append(left.pop(0))
else:
results.append(right.pop(0))
if (len(left) > 0):
results.append(left)
if (len(right) > 0):
results.append(right)
return results
print merge_sort(list('7123'))
但是我的結果看起來像這樣[['1'], ['2'], ['3'], [['7']]]
而不是['7', '1', '2', '3']
。
我的語法錯誤是什麼?