Collection type (notes) in Python __python

Source: Internet
Author: User
Tags new set shallow copy

>>>
>>> s=set (' Cheeseshoi ')
>>> s
Set ([' C ', ' e ', ' I ', ' h ', ' o ', ' s '])
>>>
>>>
>>> t=frozenset (' bookshop ')
>>>
>>> T
Frozenset ([' B ', ' H ', ' k ', ' o ', ' P ', ' s '])

Like a set in mathematical concepts, a set can do join,-, including, subsets, and superset of operations.

Len (s) return the cardinality of set S. X in s Test x for membership in S. X not in S Test X for non-membership in S. Issubset (Other) ¶set <=, other, Test whether every element in the set is. Set < other Test whether the "set is" a true subset of other, which is, set <=, and set!=. Issuperset (Other) ¶set >=, other than Test whether every element in the the the set. Set > Other Test whether the "set is" a true superset of other, which is, set >=, and set!=. Union (Other, ...) ¶set | Other | ...

Return a new set with elements from the set and all others.

Changed in version 2.6:accepts multiple input iterables. Intersection (Other, ...) ¶set & other & ...

Return a new set with elements common to the set and all others.

Changed in version 2.6:accepts multiple input iterables. Difference (other, ...) ¶set-other-...

Return a, new set with elements in the set, are not in the others.

Changed in version 2.6:accepts multiple input iterables. Copy () ¶return a new set with a shallow copy of S.
>>>
>>> set (' Posh ') ==set (' Shop ')
True
>>>
>>> set (' Shop ') <set (' Cheeseshop ')
True
>>>
>>> set (' Bookshop ') >=set (' Shop ')
True
>>>
Collection type operator: For all collection type Union () or |: Set merge set & Set intersection intersection (). -: Sets the difference set relative to the former, Difference () symmetric_difference () ^ Symmetric difference

Note If the above operator has a variable set on the left and an immutable set on the right, the result is a variable set, otherwise immutable set

The mathematical implication of the above concept can be found in discrete mathematics.

The following operators apply only to variable collections:

(Union) Update (|=)
This update method adds (possibly multiple) members from an existing collection, and this method is equivalent to update ().
>>> s = set (' Cheeseshop ')
>>> u = frozenset (s)
>>> s |= set (' PyPI ')
>>> s
Set ([' C ', ' e ', ' I ', ' h ', ' o ', ' P ', ' s ', ' Y '])
Retention/intersection update (&=)
The hold (or intersect update) operation retains a shared member of the other collection. This method is equivalent to Intersection_update ().
>>> s = set (U)
>>> s &= set (' Shop ')
>>> s
Set ([' H ', ' s ', ' o ', ' P '])
Difference Update (–=)
The difference update operation for set S and T is s-=t, and the difference update operation returns a collection in which the members of the set S go
Removes the remaining elements after the elements in the set T. This method is equivalent to Difference_update ().

>>> s = set (U)
>>> S-= set (' Shop ')
>>> s
Set ([' C ', ' e '])


symmetric differential Update (^=)
Symmetric differential update operations (S^=T) are performed on sets S and T, and the symmetric differential update operation returns a collection of
The member is only the original set S or only one of the other set T. This method is equivalent to Symmetric_difference_update ().
>>> s = set (U)
>>> t = frozenset (' bookshop ')
>>> s ^= t
>>> s
Set ([' C ', ' B ', ' e ', ' K '])

Built-in methods for collections: (All Collection methods)

Method Name Action
S.issubset (t) returns True if S is a subset of T, otherwise returns false
S.issuperset (t) returns true if T is a superset of S, otherwise returns false
S.union (t) returns a new collection that is the set of S and T
S.intersection (t) returns a new collection that is the intersection of S and T

S.difference (t) returns a new collection that is a member of S, but not a member of T
S.symmetric_difference (t) returns a new collection that is a member of S or T, but not a member shared by S and T
S.copy () returns a new collection, which is a shallow copy of the set S

The following content is reproduced:

Set since the introduction of python2.4, after a number of developments, in 2.6 including built-in data types set, Frozenset and dictionary-based set, Immutableset, the latter as a previous version of compatibility, in 2.6 has not been advocated for use. There are already a lot of articles about set and Fronzset on the web, but there is little to compare the efficiency of the existing articles for set and fronzenset when they are used respectively. This article will analyze from two angles of actual test and source analysis.
One can only use set
When the collection object is changed (for example, adding, deleting elements, and so on), you can only use the set
Two can only use Frozenset
Set can be used where Fronzet is generally used, except for the following two cases:
(1) To use as a hash object
The most essential difference between set and Frozensdet is that the former is mutable and the latter is immutable, which causes the set to be not hashed and the frozenset to be hashed. Therefore, when you need to use the collection as a key value for a dictionary, and so on, you need a hash object, only the Frozenset
(2) Do not want the collection to be changed
Most of the internal types in Python, including the list, are passed as references in parameter passing, so that the function's changes to incoming parameters refer to the parameters themselves. We need to use Frozenset when we don't want the function to change the set
.

Comparison of operating efficiency between three set and Frozenset
Our husband is a 10w list, and then we use set and Frozenset to construct the set and run 1w times. It is found that the data fluctuates greatly through many tests. It is not possible to tell which is the high efficiency of set and Frozenset.
Four source analysis
Since the direct test can not get the high and low points, then we start from the source to analyze. Python's built-in built-in implementations are in object, set and Frozenset, both of which are implemented in OBJECT/SETOBJECT.C.
By looking at Pyset_type and pyfrozenset_type you can know that set uses set_new allocation space, Set_init to initialize the collection, Frozenset allocates space with frozenset_new, and does not provide initialization functions. The set and Frozeset Tp_alloc are the same as the Tp_free functions, Pytype_genericalloc and Pyobject_gc_del respectively. Because Fronzeset does not provide initialization functions, the initialization of the data is performed synchronously while allocating space.
(a) Set initialization
The Set_new function directly invokes the Make_new_set afterlife to become the new collection request space (the second parameter passed to Make_new_set is null). There are two ways to allocate space: 1 If there are idle objects in the Global Pysetobject array (which can hold 80), assign the last object directly, and then return the data to 2 otherwise use the Pytype_genericalloc function to allocate space from the memory pool. The Pytype_genericalloc function is a generic type-allocation function for Python.
When the Set_new function completes, the Set_init function is invoked. In the Set_init function, first call set_clear_internal to free the original object in the space. Then call set_update_internal to add the object. Because this function is also called in Frozenset initialization, the function does not affect the two types of initialization efficiently, and we do not discuss the function in depth here. After the Set_init function completes, the set initialization ends
(ii) Frozenset initialization
Frozenset_new will judge the incoming iterator and return the iterator itself if the Fronzenset object is passed in. Otherwise it would call Make_new_set to request space for the new collection, but instead it would pass the incoming iterator to the Make_new_set function. After Make_new_set completes the same work as mentioned in set, it determines whether the iterator is empty and calls Set_update_internal to add the object if it is not empty.
The above analysis shows that the set_clear_internal function is called more than once during set initialization, so the frozenset will theoretically be slightly faster than set, but because the call to the function has nothing to do with the size of the incoming iterator, Other factors in the actual operation are much larger than the function call, so we can assume that the speed of set and Frozenset is the same. In both cases, it is recommended that you use Frozenset to avoid accidentally modifying the data.

As we know earlier, set initialization will call a function that clearly contains the actual contents of the table, so why does it do so? We will explain this later in the article.

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.