Python list deduplication

Source: Internet
Author: User

Suppose you have a list in Python that looks like this:

[
'A'
,
'B'
,
'A'
]

# Or like this:

[
1
,
2
,
2
,
2
,
3
,
4
,
5
,
6
,
6
,
6
,
6
]

And you want to remove all duplicates so you get this result:

[
'A'
,
'B'
]

# Or

[
1
,
2
,
3
,
4
,
5
,
6
]

How do you do that? ... The fastest way? I wrote a couple
Alternative implementations and did a quick benchmark loop on
Various implementations to find out which way was the fastest. (I
Haven't looked at memory usage). The slowest function was 78 times
Slower than the fastest function.

However, there's one very important difference between the various
Functions. Some are order preserving and some are not. For example, in
An order preserving function, apart from the duplicates, the order is
Guaranteed to be the same as it was inputted. Eg,uniqify([1,2,2,3])==[1,2,3]

Here are the functions:

Def F1 (SEQ): <br/> # Not order preserving <br/> set ={}< br/> map (set. _ setitem __, seq, []) <br/> return set. keys () <br/> def F2 (SEQ): <br/> # order preserving <br/> checked = [] <br/> for e in seq: <br/> If e not in checked: <br/> checked. append (e) <br/> return checked <br/> def F3 (SEQ ): <br/> # Not order preserving <br/> keys ={}< br/> for e in seq: <br/> keys [e] = 1 <br/> return keys. keys () <br/> def F4 (SEQ): <br/> # order preserving <br/> nodupes = [] <br/> [nodupes. append (I) For I in seq if not nodupes. count (I)] <br/> return nodupes <br/> def F5 (SEQ, idfun = none ): <br/> # order preserving <br/> If idfun is none: <br/> def idfun (x ): return x <br/> seen ={}< br/> result = [] <br/> for item in seq: <br/> marker = idfun (item) <br/> # In old Python versions: <br/> # If seen. has_key (Marker) <br/> # But in new ones: <br/> If marker in seen: Continue <br/> seen [Marker] = 1 <br/> result. append (item) <br/> return result <br/> def F6 (SEQ): <br/> # Not order preserving <br/> set = set (SEQ) <br/> return list (SET)

And what you 've all been waiting for (if you're re still reading). Here
Are the results:

*
F2
13.24

*
F4
11.73

*
F5
0.37

F1
0.18

F3
0.17

F6
0.19

(*
Order
Preserving
)

Clearlyf5
Is the "best" solution. Not only is it really
Really fast; it's also order preserving and supports an optional
Transform function which makes it possible to do this:

>>>
A
=
List
(
'Abeee'
)

>>>
F5
(
A
)

[
'A'
,
'B'
,
'E'
,
'E'
]

>>>
F5
(
A
,
Lambda
X
:
X
.
Lower
())

[
'A'
,
'B'
,
'E'
]

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.