How do you order the list, no repeat numbers?

Asked

Viewed 90 times

-1

I was given this list to sort without repeating numbers. What would be the best way to do?

lst = [54,54,93,93,77,77,44,44]

def bubbleSort(lst1):
    for x in range(len(lst1)-2,0,-3):
        for y in range(x):
            if lst1[y]>lst1[y+1]:
                temp = lst1[y]
                lst1[y] = lst1[y + 1]
                lst1[y+1] = temp            


print(lst)
bubbleSort(lst)
print("Ordenado ....")
print(lst)
  • How so order without repeats ? Do you want to sort or do you want to remove the repeaters ? Or do both wing actions separately ?

1 answer

3

One way to do this is to transform your list in a set, which is a type of list whose characteristic is to have unique elements, then use the function sorted that will return you a list orderly

lst = [54,54,93,93,77,77,44,44]

# Transformar em set
lst = set(lst)
print('set',lst)

# aplicar o sort
lst = sorted( lst ) # crescente
#lst = sorted( lst , reverse=1 ) # decrescente

print('list',lst)

Online example

Browser other questions tagged

You are not signed in. Login or sign up in order to post.