需要用最後一個元素排序元組列表與去年元素
def sort_last(tuples):
sorted_list = []
i = 0
i2 = 1
while True:
if len(tuples) == 2:
if tuples[i][-1] < tuples[i2][-1]:
sorted_list.append(tuples[i])
sorted_list.append(tuples[i2])
break
else:
sorted_list.append(tuples[i2])
sorted_list.append(tuples[i])
break
elif tuples[i][-1] < tuples[i2][-1]:
i2 += 1
print 1
elif i == i2:
i2 += 1
elif i2 == len(tuples)-1:
if tuples[i][-1] < tuples[i2][-1]:
sorted_list.append(tuples[i])
del tuples[i]
i = 0
i2 = 1
else:
sorted_list.append(tuples[i2])
del tuples[i2]
i = 0
i2 = 1
elif len(tuples) <= 1:
return tuples
else:
i += 1
return sorted_list
排序給出元組列表隨着元組列表的像([(1, 3), (3, 2), (2, 1)])
或([(2, 3), (1, 2), (3, 1)])
返回正確:([(2, 1), (3, 2), (1, 3)])
和([(3, 1), (1, 2), (2, 3)]
),但有3個以上元素的元組或4元組列表它寫入"list index out of range"
你是什麼意思與*與去年元素排序*? – EsotericVoid
爲什麼不使用'sorted'?有沒有更多的這個問題,我沒有得到? – doctorlove