我有这段代码,其中st是具有x和y参数的类的数组。我必须对它们的x或y进行排序。我如何减少它,所以我不必两次编写相同的代码(每种情况一次)?
def sort(st, coor):
for i in range(len(st)):
if coor == 'x':
selected = st[i]
j = i - 1
while j >= 0 and st[j].x > selected.x:
st[j + 1], st[j] = st[j], st[j + 1]
j -= 1
st[j + 1] = selected
i += 1
else:
selected = st[i]
j = i - 1
while j >= 0 and st[j].y > selected.y:
st[j + 1], st[j] = st[j], st[j + 1]
j -= 1
st[j + 1] = selected
i += 1
答案 0 :(得分:0)