Python: Sequence List

Source: Internet
Author: User
Tags hashable dedupe

question: How do I eliminate duplicate values while preserving the order of elements on a sequence?
Answer: If the values on the sequence are all hashable types, it is easy to use the collection or generator to solve the problem.
  EG1:  

def dedupe (items):
seen = set ()
For item in items:
If item not in seen:
Yield item
Seen.add (item)
Here is an example of using the above function:
>>> a = [1, 5, 2, 1, 9, 1, 5, 10]
>>> List (Dedupe (a))
[1, 5, 2, 9, 10]


  EG2:
This method works only when the elements in the sequence are hashable.

if you want to eliminate the elements, no, huh .If you repeat elements in a sequence like dict, you need to change the code slightlythis:
def dedupe (items, key=none):
seen = set ()
For item in items:
val = Item if key is None else key (item)
if Val not in seen:
Yield Item
Seen.add (val)
The key parameter here specifies a function that converts the sequence element to the hashable type. The following is its
usage Examples:
>>> A = [{' X ': 1, ' Y ': 2}, {' X ': 1, ' Y ': 3}, {' X ': 1, ' Y ': 2}, {' X ': 2, ' Y ': 4}]
>>> List (Dedupe (A, Key=lambda D: (d[' x '],d[' y ' )))
[{' X ': 1, ' Y ': 2}, {' X ': 1, ' Y ': 3}, {' X ': 2, ' Y ': 4}]
>>> List (Dedupe (A, Key=lambda d:d[' x '))
[{' X ': 1, ' Y ': 2}, {' X ': 2, ' Y ': 4}]

if you want to eliminate duplicate elements based on a single field, property, or a larger data structure, the second sideThe case is equally capable.

Report:

What does a hash mean?

Hash, generally translated into ' hash ', also literal translation into a ' hash ', the arbitrary length of the input through the hashing algorithm, into a fixed-length output. The output is the hash value.

This conversion is a compression map, where the space of the hash value is usually much smaller than the input space, and different inputs may be hashed to the same output, and it is not possible to determine the input value uniquely from the hash value.

Simply, a function that compresses a message of any length to a message digest of a fixed length.

Hash is mainly used in the field of information security encryption algorithm, he put some different length of information into the messy 128-bit code, called the hash value.

That is, the hash is to find a mapping between data content and data storage address

The well-known hash algorithm, MD5 and SHA1 are the most widely used hash algorithms, they are all based on MD4 design.

Python: Sequence List

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.