Python 3 Grammar Note (4): Collection

Source: Internet
Author: User
Tags set set

The collection of Python is not equal to the collection of other languages in a broad sense. Strictly speaking, a list is more like a collection, where set set is an unordered collection of non-repeating elements.

* Can be understood as, there is no value only the dictionary of keys. A dictionary of primitive form.

1 Creating a Collection
A_set = set ()             #空集合a_set = {-)

To create a collection with list:

A_set = Set ([Up, "ABC", 4])

2 Modifying a collection element
A_set.add (single Element) a_set.update (single or multiple elements or collections)  #这里可以是集合, or it can be a repeating element in the list collection, and does not increase.

  

3 删除集合元素

A_set.discard (single Element)      #删除不存在的元素, no effect A_set.remove (single element)       #删除不存在的元素, throws Exception A_set.pop () A_set.clear ()

  

4 集合操作 

A_set.union (B_set)   #并集a_set. Intersection (B_set)   #交集a_set. Difference (b_set)   #差集a_set. Issubset (b_ Set)   #检测子集

  

5 布尔检测

    • Set () represents an empty collection, false
    • Non-empty collection, True

Python 3 Grammar Note (4): Collection

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.