SoFunction
Updated on 2024-11-18

Python implementation of the removal of duplicate elements in the list of methods summarized [7 methods].

The first thing here is to give out a blog I wrote a long time ago thatPython implementation of the removal of duplicate elements in the list of the method summary [4 methodsIf you are interested, you can go and take a look, today is in the process of practice and accumulated some methods, here and summarized here.

As the content is very simple, not too much description, here directly on the code, as follows:

# !/usr/bin/env python
# -*- coding:utf-8 -*-
'''
__Author__:Yishui Cold City
Function: python list removal summary (7 methods)
'''
import sys
reload(sys)
import copy
("utf-8")
from collections import Counter
def func1(data_list):
 '''
 De-weighting using the built-in set method
 '''
 return list(set(data_list))
def func2(data_list):
 '''
 With the help of the dictionary method fromkeys
 '''
 return list({}.fromkeys(data_list).keys())
def func3(data_list):
 '''
 Using Class List Derivatives
 '''
 res_list=[]
 for one in data_list:
  if not one in res_list:
   res_list.append(one)
 return res_list
def func4(data_list):
 '''
 Using the sorted function (which is essentially still sorting using the set method)
 '''
 res_list=(data_list)
 res_list=sorted(set(data_list),key=data_list.index)
 return res_list
def func5(data_list):
 '''
 Using the 'Sort + Count' method
 '''
 result_list=[]
 temp_list=sorted(data_list)
 i=0
 while i<len(temp_list):
  if temp_list[i] not in result_list:
   result_list.append(temp_list[i])
  else:
   i+=1
 return result_list
def flagFunc(a):
 '''
 Boolean function
 '''
 if a in count_dict:
  count_dict[a]+=1
  return False
 else:
  count_dict[a]=1
  return True
def func6(data_list):
 '''
 Using the map method
 '''
 global count_dict
 count_dict={}
 tmp_list=map(flagFunc,data_list)
 return [data_list[i] for i in range(len(data_list)) if tmp_list[i]]
def func7(data_list):
 '''
 leveragecollectionsin the moduleCountermethodologies(频度过滤methodologies)
 '''
 fre_list=Counter(data_list).most_common(len(data_list))
 return [one[0] for one in fre_list]
if __name__=='__main__':
 data_list=[12,4,7,3,4,2,4,3,5,12,78,9,0,4,5,0,44,3]
 print func1(data_list)
 print func2(data_list)
 print func3(data_list)
 print func4(data_list)
 print func5(data_list)
 print func6(data_list)
 print func7(data_list)

The results are as follows:

[0, 2, 3, 4, 5, 7, 9, 12, 78, 44]
[0, 2, 3, 4, 5, 7, 9, 12, 78, 44]
[12, 4, 7, 3, 2, 5, 78, 9, 0, 44]
[12, 4, 7, 3, 2, 5, 78, 9, 0, 44]
[0, 2, 3, 4, 5, 7, 9, 12, 44, 78]
[12, 4, 7, 3, 2, 5, 78, 9, 0, 44]
[4, 3, 0, 5, 12, 2, 7, 9, 78, 44]

Sure enough it's still fun haha!

summarize

Above is the entire content of this article, I hope the content of this article for your study or work has a certain reference learning value, thank you for your support. If you want to know more about the content please check the following related links