Python code implementation Deletes a list of repeating elements

Source: Internet
Author: User

Method One: Is the use of map Fromkeys from the dynamic filtering duplicate values, map is based on hash, large array should be faster than the sort of

Method Two: is set (), set is the definition of the set, unordered, non-repeating

Method Three: Is sorted after, backwards scan, encountered the existing elements deleted

============================================================

#!/usr/bin/python
#coding =utf-8
‘‘‘
Created on 2012-2-22
Q: Given a list, remove its duplicate elements and output
‘‘‘
Def distFunc1 ():
a=[1,2,4,2,4,5,6,5,7,8,9,0]
b={}
B=b.fromkeys (a)
Print B
#print B.keys ()
A=list (B.keys ())
Print a

Def DISTFUNC2 ():
a=[1,2,4,2,4,5,7,10,5,5,7,8,9,0,3]
A=list (Set (a)) # set is a non-repeating, unordered collection. You can sort the set by using list to queue, list () to A.sort,
Print a

Def distFunc3 ():
#可以先把list重新排序, and then from the last scan of the list, the code looks like this:
list=[1,2,4,2,4,5,7,10,5,5,7,8,9,0,3]
If List:
List.sort ()
#print List
Last = List[-1]
#print Last
For I in range (len (List)-2,-1,-1):
If Last==list[i]:
Del List[i]
Else:last=list[i]



if __name__ = = ' __main__ ':
DISTFUNC1 ()
DISTFUNC2 ()
DISTFUNC3 ()

Python code implementation Deletes a list of repeating elements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.