Ok, so I am trying for a long time to do this program. My question is can i sort a portion/slice of the list using the sorted
command.
Ok for more context the aim of my program is it inputs a list of tuples that are arbitrarily arranged and sorts it first by the second element then by the first.
Say for eg. the list [(12, 2), (13, 4), (11, 1), (14, 2), (7, 2)]
is passed; the function should first sort this into [(11, 1), (12, 2), (14, 2), (7, 2), (13, 4)]
and then to [(11, 1), (7, 2), (12, 2), (14, 2), (13, 4)]
the first parts easy but I am stuck in the second part. Here's what I tried:
def Sort(l):
l = sorted(l, key=lambda l: l[1]) #for the first sort
i = 0
while i < len(l)-1:
if l[i][1] == l[i+1][1]:
z=i
y = i +1
print(z,i)
while l[z][1] == l[z+1][1] and z < len(l)-1 and y<len(l) -1 :
print(z)
if l[z][0] > l[y][0]:
(l[z],l[z+1]) = (l[z+1],l[z])
print('if',l)
y = y+1
i= i+1
i = i+1
print(l)
return l
How do I go about achieving the second part! I am new to python!