Python also includes a data type - set (set). Collection is a set of unordered elements will not be repeated. Basic features include testing and eliminating duplicate elements. Collection object supports union (joint), intersection (cross), difference (difference) and sysmmetric difference (symmetric difference) and the like math.
Create a collection set
Braces or set () function can be used to create a collection.
Parameters must be set collections need be iterator type, such as: the sequence, dictionaries, and then converted into a random set of elements will not be repeated. Since the collection is not repeated, it is possible to re-perform operations on strings, lists, tuples.
Creating an empty set
>>> s=set() >>> s set() >>> s1=set([]) #列表 >>> s1 set() >>> s2=set(()) #元组 >>> s2 set() >>> s3=set({}) #字典 >>> s3 set()
Note: If you want to create an empty set, you must use the set () {} instead. The latter is used to create an empty dictionary, a data structure that we introduce in the back.
Create a non-empty set
That list of tuples, the dictionary is not null, two examples
>>> s1=set([1,2,3,4]) >>> s1 {1, 2, 3, 4} >>> s3=set({'a':2,'b':3,'c':4}) >>> s3 {'c', 'a', 'b'}
NOTE: dictionary set transfer set, to be noted that, just take the key dictionary, the dictionary corresponds dict.keys () to set a list of transfer set.
Set of operations
Add collection
Add a set of two ways, namely, add and update. But they are when you add the element of distinction:
- add () method
to be passed as a whole an element added to the collection, such as:
>>> s=set('one') >>> s {'e', 'o', 'n'} >>> s.add('two') >>> s {'e', 'two', 'o', 'n'}
update () method
is passed to the element to split a single character, stored in the collection, and remove duplicate characters. It may be a multiple values, such as:
>>> s=set('one') >>> s {'e', 'o', 'n'} >>> s.update('two') >>> s {'e', 'n', 't', 'w', 'o'}
python development of IT technology exchange group: 887 934 385 share the source code, tutorials
Delete collection
Deletion method with the use of a set list is the same, but also remove the methods used. Such as:
- setVar.remove (element)
setVar: a set type of variable
element: pledged to find and remove the element of
function role:
Find element within the collection setVar, if there is deleted; if not found, then an error.
>>> s=set('one') >>> s {'e', 'o', 'n'} >>> s.remove('e') >>> s {'n', 'o'}
- setVar.discard(element)
setVar: a set type of variable element: pledged to find and remove the element of
function role:
Find element within the collection setVar, if there is deleted; if not found, then do nothing.
>>> sList set([1, 2, 3, 4, 5]) >>> sList.discard(1) >>> sList set([2, 3, 4, 5])
- s.pop()
s: to set a variable of type
function role:
Remove and return an element of uncertainty set type in s, if the error caused KeyError is empty.
>>> sList set([2, 3, 4, 5]) >>> sList.pop() 2
- s.clear()
s: set variable type
function operates:
all the elements in the set s Empty
>>> sList set([3, 4, 5]) >>> sList.clear() >>> sList set([])
Through the collection of
Traversal methods to traverse with exactly the same set of sequences
>> s=set('one') >>> s {'e', 'o', 'n'} >>> for i in s: print(i) ... ... e o n >>>
Another way to navigate:
>>> s=set('one') >>> s {'e', 'o', 'n'} >>> for idex,i in enumerate(s): print (idex,i) ... ... 0 e 1 o 2 n >>>
Idex variable i denotes an index of elements in a set.
Other collection methods
function | Explanation |
---|---|
only (a) | Length set of |
x in s | Test whether x is a member of s |
x not in s | Test whether x is not a member of s |
s.issubset(t) | S test whether each of the elements in the t |
s.issuperset(t) | T test whether each of the elements in s |
s.union(t) | Returns a new set contains each element of s and t |
s.intersection(t) | Returns a new set of s and t comprises elements common |
s.difference(t) | Returns a new set containing t s, but there are no elements of |
s.symmetric_difference(t) | Returns a new set of s and t does not comprise repeated elements |
s.copy() | Return set "s" of a shallow copy |
Some operators set
Since it is a collection of some of the operations that will follow a set of methods, such as the intersection of and union, difference and so on.
### intersection
Python seek intersection symbol set used is "&", even to return a collection of a set of common elements, namely the intersection of the collection.
>>> st1 = set('python') >>> st1 set(['h', 'o', 'n', 'p', 't', 'y']) >>> st2 = set('htc') >>> st2 set(['h', 'c', 't']) >>> st1 & st2 set(['h', 't'])
And set (Collection) ###
Python and seek a set of symbols using a set of "|", the two return and remove duplicate set of all elements of the set.
>>> st1 set(['h', 'o', 'n', 'p', 't', 'y']) >>> st3 = set('two') >>> st3 set(['o', 't', 'w']) >>> st1 | st3 set(['p', 't', 'w', 'y', 'h', 'o', 'n'])
Difference set
Python is set using the sign of the difference minus "-."
>>> st1 set(['1', '3', '2', '5', '4', '7', '6']) >>> st2 = set('4589') >>> st2 set(['9', '8', '5', '4']) >>> st1 - st2 set(['1', '3', '2', '7', '6'])
The results are returned in the set of elements in the set st1 st2 but not in the set.
Different set of
See differences between the two collections, -difference function used, equivalent to the difference set. Such as:
s1.difference (s3)
which is different from the new collection means with respect to the set of set s1 s3, different places, i.e. the set of all the s1, s2, rather than a collection of elements thereof.
>>> s1 set([1, 2, 3, 4, 5]) >>> s2 set([1, 2, 3, 4]) >>> s1.difference(s2) set([5]) >>> s3 set(['1', '8', '9', '5']) >>> s1.difference(s3) set([1, 2, 3, 4, 5])
Range of the set is determined
Collection can be greater than (>), less than (<), greater than or equal to (> =), less than or equal to (<=), equality (==), not equal (! =) To determine whether a set of other fully contained set, can be set using the sub parent judging function.
It defines three sets s1, s2, s3:
>>> s1=set([1, 2, 3, 4, 5]) >>> s2=set([1, 2, 3, 4]) >>> s3=set(['1', '8', '9', '5'])
Greater than (>) is greater than or equal to (> =)
>>> s1 > s2 True >>> s1 > s3 False >>> s1 >= s2 True
Represents a set of left entirely contains the right collections, such as whether the collection comprises a collection of fully s1 s2.
Less than (<) or less than or equal (<=)
>>> s2 < s1 True >>> s1 < s3 False >>> s3 < s1 False
Indicating whether the left set to the right set of fully contained, as set s1 is completely contained in the set s2.
Equals (==), not equal (! =)
>>> s1 == s2 False >>> s2 == s3 False >>> s1 != s2 True
Determining whether the two sets are identical.
Immutable collection frozenset
Python, there is a set of immutable, it is frozenset, unlike the set collection, you can add elements that were removed in the collection, the contents of the collection is immutable, like strings, tuples.
>>> f = frozenset() >>> f frozenset([]) >>> f = frozenset('asdf') >>> f frozenset(['a', 's', 'd', 'f']) >>> f = frozenset([1,2,3,4]) >>> f frozenset([1, 2, 3, 4]) >>> f = frozenset((1,2,3,4)) >>> f frozenset([1, 2, 3, 4]) >>> f = frozenset({1:2, 'a':2, 'c':3}) >>> f frozenset(['a', 1, 'c'])
If you try to change the immutable elements of the collection, it will be reported AttributeError error.
Immutable collections, in addition to the content can not change, the same as other functions and operations with a variable collection set.