Sorting a list in Python containing tuples with Selection Sort -
i sort following list using selection sort:
list1 = [(1,2,3),(3,6,4),(4,7,9)]
how can sort above second element of each tuple (2,6,7) ?
i have following code normal selection sort, know how tuples.
def selection_sort(list): in range(len(list)): mini = min(list[i:]) min_index = list[i:].index(mini) list[i + min_index] = list[i] list[i] = mini
you can tell min
use second element comparisons:
mini = min(list[i:], key=lambda l: l[1])
btw, it's bad call variable list
because can't use python's own list
anymore , because you're confusing everyone. also, didn't make list tuple, preventing in-place sorting you're trying.
Comments
Post a Comment