3 ways to remove weight from Python list elements

Source: Internet
Author: User
Tags python list

Before the interview encountered this problem, today busy finishing the following, probably think of the following three ways.


<span style= "FONT-SIZE:18PX;" >class delect_duplicate:def Method_set (self,mlist): Print ("Method_set is called") print ("Before Proce SS Mlist is ", mlist) L2 = set (mlist) print (" After processed by Method_xunhuan, the mlist is ", L2) def meth Od_xunhuan (self,mlist): Print ("Method_xunhuan is called") print ("Before process mlist is", mlist) if                Mlist:mlist.sort () last = mlist[-1] for i in range (len (mlist)-2,-1,-1): If last = = Mlist[i]: del mlist[i] else:last = mlist[i] pri        NT ("After processed by Method_xunhuan, the mlist was", mlist) def method3 (self,mlist): Print ("method3 is called")        Print ("Before process mlist is", mlist) temp = [] [Temp.append (i) for I in mlist if not I in temp] Print ("After processed by METHOD3, the result is", temp) </span>

The first method directly with the set method, simple and rough, but because too simple, often can not meet the interviewer,

The second method is to sort the elements of the list and then compare them back and forth, removing the same elements, but the first two methods have one drawback, that is, the position of the element is changed after processing.

The third method is to use two lists for processing, without changing the location of the element, the test code is as follows:

<span style= "FONT-SIZE:18PX;" >if __name__ = = ' __main__ ':    A = [1, 5, 4, 8, 9, 2, 4, 5, 1]    B = [1, 5, 4, 8, 9, 2, 4, 5, 1]    C = [1, 5, 4, 8 , 9, 2, 4, 5, 1]    s = delect_duplicate ()    s.method_set (A)    S.method_xunhuan (B)    s.method3 (C) </span>

The results of the operation are as follows:

<span style= "FONT-SIZE:18PX;"  >method_set is Calledbefore process mlist are [1, 5, 4, 8, 9, 2, 4, 5, 1]after processed by Method_xunhuan, the mlist is {1, 2, 4, 5, 8, 9}method_xunhuan is Calledbefore process mlist are [1, 5, 4, 8, 9, 2, 4, 5, 1]after processed by Method_xu Nhuan, the mlist is [1, 2, 4, 5, 8, 9]method3 are Calledbefore process mlist is [1, 5, 4, 8, 9, 2, 4, 5, 1]after processed By METHOD3, the result is [1, 5, 4, 8, 9, 2]</span>

Complete code: Https://github.com/wlseu/delectdumplicate





3 ways to remove weight from Python list 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.