subject : List to remove duplicate elements , And keep the original order

Method 1 :

# The list to be processed 
L1 = [111,44,55,33,22,11] # Using the collection set Properties of , duplicate removal
s1 = set(L1) # Turn a set into a list
L2 = list(s1) # according to L1 Medium index Sort
L2.sort(key = L1.index) # Get a list that de duplicates and keeps sorting
print(L2)

Method 2 :

# The list to be processed 
L1 = [111,44,55,33,22,11] # Define an empty list
L2 = [] # By traversing L1 list , The process of traversal follows L1 The order of arrangement
# Values that will not be repeated , In turn into L2 in
for i in L1:
if i not in L2:
L2.append(i) # Get no repeat , And keep L1 A new list of sorts
print(L2)

Python Repeat the list of interview questions and keep the original order of more related articles

  1. Python The list of interview questions is out of range

    # The values in the following list are out of range , Error or return value : L1 = [',]print(L1[10]) print(L1[10:]) The first print will report an error : The second print returns an empty list

  2. python Use set Duplicate the list , And keep the list in its original order

    # python Use set Duplicate the list , And keep the list in its original order list1 = ['cc', 'bbbb', 'afa', 'sss', 'bbbb', 'cc', 'shafa'] for item i ...

  3. Python There is a sequence table for the confusion of interview questions

    # It's required to scramble a sequence table # Pending list L1 = [11,22,33,44,55,66,77,] Method : # introduce random modular , The shuffle See the figure below import random L1 ...

  4. Python Interview questions -- Summary

    Link to the original text -https://github.com/taizilongxu/interview_python Python Linguistic characteristics 1 Python Function parameter transfer of 2 Python The metaclass in (metacla ...

  5. python Interview questions

    Question 1 : What the output of the following code will be ? Say your answer and explain . 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Parent(object):     x = 1   clas ...

  6. Python One of the interview questions Python Function default parameters trap

    Please see the following procedure : def extend_list(v, li=[]): li.append(v) return li list1 = extend_list(10) list2 = extend_l ...

  7. Python An analysis of the depth of the interview questions

    # copy.copy Shallow copy Copy only the parent object , Does not copy the inner child of an object . ** It's just a simple pointer assignment # copy.deepcopy Deep copy Copy objects and their children ** Pointer assignment , And content copy Take a simple example ...

  8. Python One of the interview questions Python The copy and assignment of

    python It's a reference variable structure , That is to say, if you assign a value to a variable , Instead of opening up a memory space for this variable, we tell the variable the memory space address of an object , The advantage of this is that it's easy to manage , Save memory space , Easy to release memory and so on . but ...

  9. Python One of the interview questions ( in the light of dict perhaps set data type ) Edge ergodic Edge modification Report errors dictionary changed size during iteration

    # result It's a dictionary , Put the value of the attribute in it as None The attribute of is deleted for key in result: if not result[key]: del result[key] continue But the newspaper ...

Random recommendation

  1. firefox, Cross domain ajax Calling method

    stay A Site ajax call B Website page ( Method ) when , Use post, And dataType The type is jsonp Sometimes in IE There will be No Transport Error prompt for , Please add jQuery.support.cors = tr ...

  2. java Simulate user login ( Exclude the case of no captcha , Grab Web Information )

    import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import j ...

  3. Java What's the use of generics

    1 Generics give the ability of type parameter polymorphism 2 The first advantage of generics is strict type checking at compile time , Improve the security and robustness of the program , This is the most important feature of the set framework . 3 Generics eliminate most type conversions . Let's say there's no generics , When you use the set framework ...

  4. Factory method model (Factory Method Pattern)

    Overview of the factory approach pattern Factory method pattern is a kind of design pattern which is developed to make up for the shortcomings of simple factory pattern and inherit its advantages , Belong to GoF One of the . It can better meet the requirements of the open close principle . Definition : Defines an interface for creating objects , But let the subclass decide ...

  5. from ELK To EFK Evolution of

    background As the largest online education site in China , At present, the users of Hujiang log service include online school , transaction , Finance ,CCTalk Search and analyze the log of multiple products of multiple departments , There are more than ten kinds of daily logs , Deal with the appointment every day 10 Billion bars (1TB) journal , Heat number ...

  6. CSS3 attribute -webkit-font-smoothing Font antialiasing rendering

    Anti aliasing rendering can make the font look clearer and more comfortable . Today, Icon Font has become a trend , Antialiasing rendering is also used more and more . font-smoothing Yes no standard CSS Definition . It was included in the draft of the Standard Specification , Later, for some reasons, from ...

  7. C# Use protobuf Serializing and deserializing objects

    protobuf yes google An open source project of , It can be used for two purposes : (1) Data storage ( Serialization and deserialization ), Be similar to xml.json etc. : (2) Make network communication protocol . Source code download address :https://gi ...

  8. Git Using code Repositories on different platforms at the same time

    Problem description The company's project use code warehouse is gitinn/gitlab etc. , Personal project use github For hosting , The mailbox and user name of company project and personal project are different , and ssh The key pair of is based on these two information , So I want to be with ...

  9. second level C Notes on the real topic of language

    second level C Notes on the real topic of language 1. Knowledge focus : data type . loop . Array . function . The pointer . Structure and community 2. Find the running result of the program #include <stdio.h> main() {     short i ...

  10. Share a date based constellation PHP function

    In fact, the principle is very simple , That is to store all constellation month date ranges in an array , Then judge which range it belongs to according to the date , So we can get which constellation it is . The following function is more concise , You can refer to it function constellat ...