Python Road -08-Collection

Source: Internet
Author: User

A collection is an unordered, non-repeating combination of data, and its main functions are as follows:

    • Go to the weight, turn a list into a set, and then automatically go heavy.
    • Relationship test, test the intersection of two sets of data, difference set, and the relationship between the set

8.1 Integrated operation

8.1.1 Common operations

#! Author:lanhan
List_1 = [1,4,5,7,3,6,7,9]
List_1 = Set (list_1)
Print (List_1,type (list_1))
List_2 = Set ([2,6,0,66,22,8,4])
Print (list_1,list_2)
List_3 = Set ([1,3,7])
#交集
Print"Intersection", List_1.intersection (list_2))
Print (List_1 & list_2)
#并集 (Remove the same)
Print"the Set", List_1.union (list_2))
Print (list_2 | list_1)
#差集 (some in list_1, not in list_2)
Print"Difference Set", List_1.difference (list_2))
Print (list_1-list_2)
Print"Difference Set", List_2.difference (list_1))
Print (list_2-list_1)
#子集 ()
Print"Subset", List_3.issubset (list_1))
#父集 ()
Print"Parent Set", List_1.issuperset (List_3))
#对称差集 (none of the other)
Print"Symmetric difference Set", List_1.symmetric_difference (list_2))
Print (list_1 ^ list_2)
# # #m没有交集则返回true
List_4 = Set ([5,6,8])
Print"return true without intersection ", List_3.isdisjoint (List_4))

8.1.2 Basic Operation

#! Author:lanhan
#添加1项
List_4 = Set ([1,3,7])
List_4.add ('a ')
Print (List_4)
#添加多项
List_4.update ([120,130])
Print (List_4)
#使用remove () can delete an item
List_4.remove (7)
Print (List_4)
#删除, if there is no value, no error
Print (List_4.discard ("ddd"))
Length of #set
Print (len (list_4))
#测试1是否是list_4的成员
Print (1 in (list_4))
#测试2是否不是list_4的成员
Print (2 not in (List_4))
#浅复制
List_5 = List_4.copy ()
Print (list_5)

Python Road -08-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.