Python uses a collection instead of a list

Source: Internet
Author: User


"""
Description: For a specified sequence, use the following algorithm if you need to obtain a sequence that contains only the sequences that are not duplicates:
"""
Seq=[' A ', ' a ', ' B ', ' C ', ' C ', ' d ']
Res=[]
For El in SEQ:
If El not in res:
Res.append (EL)
Print Res

Or

In the Res list, use in to find, its highest complex height will be 0 (n), and then call it in the global loop, and its complexity is also 0 (n), so the complexity is usually 2 square.

Using the Set () type for such an operation is faster because it uses a hash such as dict to find the stored value, that is, the time it takes to query whether it is in set will be a constant:

as follows: Set (SEQ)

Note: When trying the complexity of an algorithm, the data structure should be carefully considered, there are many built-in types, to choose the right one, before invoking the algorithm, the transformation of the database is often more than based on the original data modification algorithm to make it faster and better!

Python uses a collection instead of a 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.