Knowledge about set types in Python, python set

Source: Internet
Author: User

Knowledge about set types in Python, python set

Set Type
In mathematics, set is called a set composed of different elements. set members are usually called set elements ). Python introduces this concept to its set type object. A set object is a group of unordered hash values. A set Member can be used as a dictionary key. The Set object for converting a mathematical set to Python is very effective. The set link test and operators such as union and intersection also work in Python as we expected.
Like other container types, the set supports checking members using the in and not in operators. The len () built-in function obtains the base number (size) of the Set and iterates the members of the set using the for loop. However, because the set itself is unordered, you cannot create an index for the set or perform the slice operation for the set. There is no key (keys) to obtain the value of the element in the set.
Set has two different types: set and frozenset ). For a set, you can add or delete elements. For a frozenset, this is not allowed. Note: A set is not hashable. Therefore, it cannot be used as a dictionary or an element in other sets. Immutable set
(Frozenset) is the opposite, that is, they have hash values and can be used as Dictionary keys or as a member in the set.
Set operators and relational symbols:

Actual operations of the set type:

(1) How to Create a set type and assign values to the set
The set is different from the List ([]) and Dictionary ({}), and there is no special syntax format. The list and dictionary can be created by using their own factory methods list () and dict () respectively. This is also the only way to create a set: Use the factory method set () of the set () and frozenset ().

  >>> s = set('cheeseshop')   >>> s   set(['c', 'e', 'h', 'o', 'p', 's'])   >>> t = frozenset('bookshop')   >>> t   frozenset(['b', 'h', 'k', 'o', 'p', 's'])   >>> len(s), len(t)   (6, 6)   >>> s == t   False 

(2) how to access values in a set
You can traverse to view the set members or check whether an element is a member of a set.

  >>> 'k' in t   True   >>> for i in s:   ...   print i   ...    c   e   h   o   p   s 

(3) how to update a set
Add and delete members of a set using built-in methods and operators of various sets. An exception occurs when you try to modify an immutable set only when the set can be modified.
[Html] view plaincopy view CODE snippets derived from my CODE snippets on CODE

  >>> s.add('z')   >>> s   set(['c', 'e', 'h', 'o', 'p', 's', 'z'])   >>> s.update('pypi')   >>> s   set(['c', 'e', 'i', 'h', 'o', 'p', 's', 'y', 'z'])   >>> s.remove('z')   >>> s -= set('pypi')   >>> s   set(['c', 'e', 'h', 'o', 's']) 

(4) how to delete members and collections in a set
Delete the remove () built-in method of the Set member. To delete a set itself, you can delete it out of its scope or call del to clear the current namespace just like deleting any Python object. If its reference count is zero, it will also be marked for garbage collection. Such as del s.

Instance:
Use the set factory methods set () and frozenset ():

  >>> s = set('cheeseshop')   >>> s   set(['c', 'e', 'h', 'o', 'p', 's'])   >>> t = frozenset('bookshop')   >>> t   frozenset(['b', 'h', 'k', 'o', 'p', 's'])   >>> type(s)   <type 'set'>   >>> type(t)   <type 'frozenset'> 

How to update a set and use the built-in methods and operators of various sets to add and delete members of the Set:

  >>> s.add('z')   >>> s   set(['c', 'e', 'h', 'o', 'p', 's', 'z'])   >>> s.update('pypi')   >>> s   set(['c', 'e', 'i', 'h', 'o', 'p', 's', 'y', 'z'])   >>> s.remove('z')   >>> s   set(['c', 'e', 'i', 'h', 'o', 'p', 's', 'y'])   >>> s -= set('pypi')   >>> s   set(['c', 'e', 'h', 'o', 's']) 

 

Delete collection

  del s 

Member relationship (in, not in)

  >>> s = set('cheeseshop')   >>> t = frozenset('bookshop')   >>> 'k' in s   False   >>> 'k' in t   True   >>> 'c' not in t   True 

Set equivalence/non-equivalence

  >>> s == t   False   >>> s != t   True   >>> u = frozenset(s)   >>> s == u   True   >>> set('posh') == set('shop')   True 

The difference or relative supplement set of the difference (s and t) sets (-) refers to a set C. The elements in the set only belong to the set s, not
In set t. The difference symbol has an equivalent method,

  difference().   >>> s - t   set(['c', 'e'])    

Symmetric Difference (^): symmetric difference is the XOR of the set. The above article describes the practical application steps of the Python dictionary for the set type.

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.