preamble
Hello, hello, hello. Good evening, everyone. I'm back with another tip for you.
When a python list has duplicate elements, there are several ways to remove them
If you think it's good, learn how to use it !
Delete by traversing the list directly
l1 = [1, 1, 2, 2, 3, 3, 3, 3, 6, 6, 5, 5, 2, 2] for el in l1: if (el) > 1: (el) print(l1)# will be missed because when an element is deleted, the following element is moved forward, causing the immediately following element to be skipped.
Delete by traversing the index
l1 = [1, 1, 2, 2, 3, 3, 3, 3, 6, 6, 5, 5, 2, 2] for el in range(len(l1)): # At this point, len(l1) has been determined, and will not change as l1 changes. if (l1[el]) > 1: (l1[el]) print(l1) # will report an error, because the deletion of the element causes the length of l1 to become shorter, but for traversal of the previous index length, will cause the index to exceed the range and report an error
Delete the original list by traversing the created slices
l1 = [1, 1, 2, 2, 3, 3, 3, 3, 6, 6, 5, 5, 2, 2] for el in l1[:]: if (el) > 1: (el) # There's no problem, you can de-duplicate, but you can't preserve the original order # print(l1)
Record the elements to be retained in a new list
l1 = [1, 1, 2, 2, 3, 3, 3, 3, 6, 6, 5, 5, 2, 2] lst = [] for el in l1: if (el) < 1: (el) print(lst) # No problem, also preserves the original order, but creates a new list.
Delete backwards through the index
l1 = [1, 1, 2, 2, 3, 3, 3, 3, 6, 6, 5, 5, 2, 2] for el in range(len(l1)-1, -1, -1): if (l1[el]) > 1: (el) # No problem, and the original order is preserved # # (l1[el]) # No problem, but you can't keep the original order. # del l1[el] # This will keep the original order, you can think about why. print(l1)
Delete by recursive function
l1 = [1, 1, 2, 2, 3, 3, 3, 3, 6, 6, 5, 5, 2, 2] def set_lst(lst): for el in lst: if (el) > 1: (el) set_lst(lst) # Each time a new function is opened, determine the list after the last time an element was deleted. else: # Until the end, the elements in the list are all one, run else return lst print(set_lst(l1)) # Since the deletion starts from the front, the original order is not preserved. ''' [1, 1, 2, 2, 3, 3, 3, 3, 6, 6, 5, 5, 2, 2] [1, 2, 2, 3, 3, 3, 3, 6, 6, 5, 5, 2, 2] [1, 2, 3, 3, 3, 3, 6, 6, 5, 5, 2, 2] [1, 3, 3, 3, 3, 6, 6, 5, 5, 2, 2] [1, 3, 3, 3, 6, 6, 5, 5, 2, 2] [1, 3, 3, 6, 6, 5, 5, 2, 2] [1, 3, 6, 6, 5, 5, 2, 2] [1, 3, 6, 5, 5, 2, 2] [1, 3, 6, 5, 2, 2] [1, 3, 6, 5, 2] return lst = [1, 3, 6, 5, 2] '''
There is no doubt that set() is the most convenient
l1 = [1, 1, 2, 2, 3, 3, 3, 3, 6, 6, 5, 5, 2, 2] lst = list(set(l1)) print(lst)
summarize
to this article on Python to remove duplicate elements in the list of seven ways to give examples of the article is introduced to this, more related Python to remove duplicate elements of the list of content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!