Python provides three common methods for deleting duplicate elements in the list.
This example describes three common methods for deleting duplicate elements in the Python list. We will share this with you for your reference. The details are as follows:
If a list is specified, the repeated elements in the list must be deleted.
ListA = ['python', 'yue', 'yan', 'yes', 'yi', 'Gateway', 'Action', 'status', 'yue ', 'yan']
Method 1To sort the list, and compare two adjacent elements from the end in sequence. If duplicate elements exist, delete them. Otherwise, move the pointer to the left to repeat the above process:
def deleteDuplicatedElementFromList(list): list.sort(); print("sorted list:%s" % list) length = len(list) lastItem = list[length - 1] for i in range(length - 2,-1,-1): currentItem = list[i] if currentItem == lastItem: list.remove(currentItem) else: lastItem = currentItem return list
Method 2, Set a temporary list to save the result and traverse the original list from the beginning. If the temporary list does not contain the current element, append it:
def deleteDuplicatedElementFromList2(list): resultList = [] for item in list: if not item in resultList: resultList.append(item) return resultList
Method 3To convert the list into a set by using the unique feature of elements in python, and then convert it to a list and return:
def deleteDuplicatedElementFromList3(listA): #return list(set(listA)) return sorted(set(listA), key = listA.index)
Execution result:
Print (deleteDuplicatedElementFromList (listA) # sorted list: ['python', '1', 'dynamic ', 'status', 'yes', 'yan', 'yan ', 'output', 'output'] # ['python', 'yi', 'in', 'status', 'yes', 'any ', 'out', 'ware'] print (deleteDuplicatedElementFromList2 (listA) # ['python', 'out', 'yes', '1 ', 'Gateway', 'mobile', 'status'] print (deleteDuplicatedElementFromList3 (listA) # ['python', 'out', 'any', 'yes ', '1', 'door', 'Action', 'status']
Analysis:
Method 1: The logic is complex. Saving values of temporary variables consumes memory. The returned results destroy the original list order, with the worst efficiency.
Method 2: directly call the append method to modify the list. The logic is clear and the efficiency is secondary.
Method 3: extremely concise, with the highest efficiency in using python native Methods
PS: Here are several repeated tools for your reference:
Online deduplication tools:
Http://tools.jb51.net/code/quchong
Online text deduplication tool:
Http://tools.jb51.net/aideddesign/txt_quchong