Python Learning 5-collection and collection operations

Source: Internet
Author: User

A set of mathematical meanings, without repeating elements, which must be hashed
1. Definition
s = Set ()
s = {A-i}
s = Set (can iterate over objects)
2, plus
S.add (Element) #增加1个元素, similar to a list of append
S.update (an iterative object) #增加一个可迭代对象, similar to a list of extend
3. Delete
S.remove (Element) #元素不存在, quote key error
S.pop () #随机删除并返回1个元素, when the collection is empty, report key error
S.discard (Element) #删除不存在元素的时候, no error
S.clear () #清空集合
4. A collection cannot modify a single element
5. Find
Collection cannot be indexed for lookup
Collection cannot access a single element
Collection No Lookup method
The collection is not a linear structure
The collection element has no order
6. Member operators: Determine if an element is in a container
In: Returns True
Not in: does not return true
When the data size is very large, the members of the collection are much more efficient than the list, and the scale is irrelevant; The list is related to its size, and the list is much faster when the scale is small.
7, set of operations
S1.intersection (S2) #交集
S1.intersection_update (S2) #交集并修改s1
S1 & S2 #求交集
S1.difference (S2) #差集
S1.difference_update (S2) #差集并修改s1
S1-s2 #差集
S1.symmetric_difference (S2) #对称差集
S1.symmetric_difference_update (S2) #对称差集并修改s1
S1 ^ S2 #对称差集, equivalent to XOR or
S1.union (S2) #并集
S1 | S2 #并集
8, the judgment of the set
S1.issubset (S2) #s1是否是s2的子集
S1.issuperset (S2) #s1是否是s2的超集
S1.isdisjoint (S2) #判断是否有交集, there is a false, inaction really

Python Learning 5-collection and collection 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.