Python Collection type usage analysis

Source: Internet
Author: User
This article analyzes the Python collection type usage. Share to everyone for your reference. The specific analysis is as follows:

Python's collection type is similar to other languages and is an unordered set of distinct elements that I have not seen in the other languages I've learned before, and basic features include relationship testing and eliminating duplicate elements. The collection object also supports the mathematical Operations of Union (Union), intersection (intersection), Difference (poor) and sysmmetric difference (symmetric difference sets), and is very similar to the set of mathematics in our junior high school.

First, look at the Python collection type of non-repetition, this aspect of doing some deduplication is very good, for example, we have to deal with some data, want to put the duplicate data to
It can be converted to a collection type and then converted to other types by the collection type, if it is removed and then manipulated.

A = [2,3,4,2,1]

The final effect we will achieve is:

A = [1,2,3,4]

So how are we going to make it?

Looking at this list, we find that there are duplicate elements in the list, so the first thing we think about is to get rid of the repeating elements in the table.

A = set (a) print a

The result of collection A is:

Set ([1, 2, 3, 4])

The next step is to implement the sort, and we think of a simpler method, because the collection has no sorting method, and the list has a sort method, so we convert it to the type of the Python list, and call the sort method of the list.

A = List (a) A.sort () print a

The result of listing A is:

[1,2,3,4]

Speaking of collections, you can also talk about Python tuples and Python data types summary

II. Union (Union), intersection (cross), Difference (poor)

A = set (' ABCDE ') b = Set (' BDCF ')

To find the intersection of a collection:
A & B
The result is:

Set ([' C ', ' B ', ' d '])

Differential set:
A-B
The result is:

Set ([' A ', ' e '])

Seek Union:
A|b
The result is:

Set ([' A ', ' C ', ' B ', ' e ', ' d ', ' F '])

Summary: Python collections and mathematical collections of concepts are more like, often used in data de-processing and some data transfer processing.

Hopefully this article will help you with Python programming.

  • 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.