Collection type knowledge explained in Python

Source: Internet
Author: User
Collection type
Mathematically, a set is called a set composed of different elements, and the members of the set (set) are usually called set elements. Python introduces this concept to its collection-type object. A collection object is a set of unordered, hashed values that a collection member can make as a key in a dictionary. The collection object of the mathematical set to Python is very effective, and the set-relationship test and the union, intersection, and so on are the same as we expected in Python.
As with other container types, the collection supports checking members with in and not in operators, and the Len () built-in function gets the cardinality (size) of the collection, iterating over the members of the collection with a For loop. However, because the collection itself is unordered, you cannot create an index or perform a slice (slice) operation on the collection, nor do you have a key (keys) available to get the values of the elements in the collection.
There are two different types of collections (sets): Mutable set (set) and immutable set (Frozenset). For mutable collections (set), you can add and remove elements, which are not allowed for immutable collections (Frozenset). Note: A mutable collection (set) is not a hash, so it cannot be used as a dictionary key or as an element in another collection. Immutable collections
(Frozenset) is the opposite, that is, they have a hash value that can be used as a dictionary key or as a member of a set.
Set operators and relationship symbols:

Actual operation of the collection type:

(1) How to create a collection type and assign a value to a collection
The collection differs from the list ([]) and the dictionary ({}) without a special syntax format. Lists and dictionaries can be created separately with their own factory methods list () and Dict (), which is the only way the collection is created: Set () and Frozenset () with the factory method of the collection.

  >>> s = set (' Cheeseshop ')   >>> s   set ([' C ', ' e ', ' h ', ' o ', ' P ', ' s '])   >>> t = Frozens ET (' bookshop ')   >>> t   frozenset ([' B ', ' H ', ' k ', ' o ', ' P ', ' s '])   >>> Len (s), Len (t)   (6 , 6)   >>> s = = t   

(2) How to access the values in the collection
You can traverse the View collection members or check whether an item element is a member of a collection.

  >>> ' K ' in T   True   >>> for i in S:   ...   Print I ...    C   e   h   o   P   

(3) How to update a collection
Adds and removes members of a collection using various collections of built-in methods and operators. Only mutable collections can be modified, and attempting to modify immutable collections throws an exception.
[HTML] View plaincopy on code to see a snippet derived from my Code slice

  >>> 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   

(4) How to delete members and collections in a collection
Removes the collection member from the built-in method of the Remove (). Deleting the collection itself can cause the collection to go out of its scope or call Del to clear out the current namespace just as you would any Python object. If it has a reference count of zero, it is also flagged for garbage collection. such as Del S.

Instance:
Set () and Frozenset () with the factory method of the collection:

  >>> s = set (' Cheeseshop ')   >>> s   set ([' C ', ' e ', ' h ', ' o ', ' P ', ' s '])   >>> t = Froz Enset (' bookshop ')   >>> t   frozenset ([' B ', ' H ', ' k ', ' o ', ' P ', ' s '])   >>> type (s)   
 
  
   
     >>> type (t)   
  
    
  
   
 
  

How to update collections add and remove members of a collection with methods and operators built in various collections:

  >>> 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   

Delete Collection

  

Member relationship (in, not in)

  >>> s = set (' Cheeseshop ')   >>> t = frozenset (' bookshop ')   >>> ' K ' in S   false< C20/>>>> ' K ' in T   True   >>> ' C ' isn't in T   

Set equivalence/not equivalent

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

Difference complement/relative complement set (–) The difference or relative complement of two sets (S and T) refers to a set of C, the elements of the set, which belong to the set S, but not
to the collection T. The difference symbol has an equivalent method,

  Difference ().   >>> s-t   Set ([' C ', ' e '])    

Symmetric difference (^): Symmetric difference is the set of XOR above the article is the Python Dictionary of the actual application of the collection type steps.

  • 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.