Python collection type usage analysis _python

Source: Internet
Author: User

The example in this article analyzes Python collection type usage. Share to everyone for your reference. The specific analysis is as follows:

The Python collection type, like any other language, is a unordered set of elements that I have learned before in other languages that seem to have not seen this type, and the basic features include relational testing and the elimination of duplicate elements. The collection object also supports mathematical operations such as Union (union), intersection (intersection), difference (difference) and sysmmetric difference (symmetric difference set), which is very similar to our collection of mathematics in junior high school.

First, look at the Python collection type of duplication, this is done to do some processing is very good, for example, we have to deal with some data, to the duplication of data to
removed, and then in action, you can convert it to a collection type and then convert it to another type from the collection type.

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

The effect we will end up achieving is:

A = [1,2,3,4]

So how are we going to make it happen?

Looking at this list, we find that there are duplicate elements in the list, so our first thought is to get rid of the repeating elements in our lists.

A = set (a)
print a

The result of set A is:

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

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

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

The result of List A is:

[1,2,3,4]

When it comes to collections, it's also a summary of Python tuples and Python data types.

Union (Union), Intersection (AC), Difference (poor)

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

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

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

To find the difference set:
A-b
The result:

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

Union:
a|b
The result:

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

Summary: Python sets and mathematical sets of concepts are more like, often used in the process of data reprocessing and some data transfer processing.

I hope this article will help you with your 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.