Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to de-duplicate and keep the original order of list elements in python

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces you how to list elements in python and keep the original order, the content is very detailed, interested friends can refer to, I hope to help you.

This requirement relates to collections, so let's talk about collections before we go into code. Before we talk about sets, let's review mutable and immutable data types.

Invariant data types: numeric number(including int, float, bool, complex), string, tuple.

Variable data types: list, dictionary, set.

Collection access is based on hash algorithm mapping, only immutable data types can do hash algorithm, so only immutable data types can be stored in collections. The characteristics of sets are de-duplication and disorder. The first contact when learning sets is intersection and difference. Every book that teaches python talks about sets will not miss intersection and difference, so I won't repeat it here.

So now we need to de-duplicate the elements of a list and keep the original order. What's the optimal solution?

Or go directly to the code to see the case:

raw_address = ['Beijing City', 'Beijing City', 'Changping District',' Renmin Dajie ', ' 9999']#Original address information entered by customer #Address information in database is '999Renmin Dajie, Changping District, Beijing City'. How to match address information entered by customer with address information in database? address = list (set(raw_address)) #Convert the original address list into a set and then into a list, automatically complete de-duplication print(address) #Output is ['Renmin Dajie', 'Changping District', '999',' Beijing City ']address.sort (key=raw_address.index) #Then arrange the deduplicated list in the original order print(address) #Output is ['Beijing City', 'Changping District', 'Renmin Street', '999']

As you can see, demultiplexing the elements in the list with python and keeping the original order takes only 2 lines of code to complete, and it runs very fast.

What is the principle behind this?

This is because in python variables are essentially references, and in the above case the raw_address list has 5 strings stored in different addresses of memory. rad_address list stores the memory addresses of 5 strings. When de-duplication, there is no need to check whether there are duplicates in the string pairwise matching in the list (this operation is quite time-consuming). As long as there are identical memory address references, remove the duplicate references and keep only one. When restoring the order, just sort by the index of the original list.

About how to duplicate the list elements in python and keep the original order, I hope the above content can be of some help to everyone and learn more knowledge. If you think the article is good, you can share it so that more people can see it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report