Python collection type Usage Analysis, python collection usage

Source: Internet
Author: User
Tags python list

Python collection type Usage Analysis, python collection usage

This article analyzes the usage of python set types. Share it with you for your reference. The specific analysis is as follows:

The collection type of python is similar to that of other languages. It is an unordered, non-repeating element set. I have never seen this type in other languages I have learned before, basic functions include link testing and deduplication. the Set object also supports mathematical operations such as union, intersection, difference, and sysmmetric difference, which are very similar to the set of Junior Mathematics.

1. First, let's take a look at the non-repetition of the python set type, which is very good for deduplication. For example, if we want to process some data, we want to give repeated data
Remove it, and then convert it to the set type in the operation, and then convert it from the set type to another type.

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

The final result we want to achieve is:

a = [1,2,3,4]

So how can we implement it.

Observe this list and we will find that there are repeated elements in the list, so we first think of removing the repeated elements in the list.

a = set(a)print a

The result of set a is:

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

The next step is to implement sorting. We think of a simple method, because the set does not have a sorting method, and the list has a sorting method, so we convert it to the python list type, call the sorting method of the list.

a = list(a)a.sort()print a

The result of list a is:

[1,2,3,4]

Speaking of collections, You can also talk about the summary of python tuples and python data types.

2. union, intersection, and difference)

a = set('abcde')b = set('bdcf')

Calculate the intersection of the Set:
A & B
The result is:

set(['c', 'b', 'd'])

Difference set:
A-B
The result is:

set(['a', 'e'])

Seek union:
A | B
The result is:

set(['a', 'c', 'b', 'e', 'd', 'f'])

Summary: The concept of a python set is similar to that of a mathematical set. It is often used for data deduplication and data transfer processing.

I hope this article will help you with Python programming.

Related Article

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.