Python: Collections and their operations

Source: Internet
Author: User

#集合及其运算
List_1 = Set ([1,4,5,7,3,6,7,9])
List_2 = Set ([2,6,0,66,22,8,4])
Print (list_1, list_2)
#交集
Print (List_1.intersection (list_2))
Print (List_1 & list_2)
#并集
Print (List_1.union (list_2))
Print (list_1 | list_2)
#差集
Print (List_1.difference (list_2))
Print (list_1-list_2)
#对称差集
Print (List_1.symmetric_difference (list_2))
Print (list_1 ^ list_2)
#子集
List_3 = Set ([1,3,7])
Print (List_3.issubset (list_1))
Print (List_1.issuperset (list_3))

#没有交集
List_4 = set ([100])
Print (List_1.isdisjoint (list_4))

#基本操作
#添加元素
List_1.add (#添加一项)
List_1.update ([20,30,40])
Print (list_1)
#删除元素
Print (list_1)
List_1.remove (3) #若元素不包含在集合中, will error
List_1.discard (5) #若元素不包含在集合中, no error.
List_1.pop ()
Print (list_1)
#测试是否为成员
Print (in list_1)

Python: Collections and their operations

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.