This article mainly introduces the collection type knowledge in Python, which is the basic knowledge in getting started with Python. For more information, see
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(t)
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.