@nalan90
2017-07-28T17:30:30.000000Z
字数 7294
阅读 592
Python高效编程技巧实战
常用函数
create a set
Creating an empty set is a bit tricky.
Empty curly braces {} will make an empty dictionary in Python. To make a set without any elements we use the set() function without any argument.
In [90]: s1 = {1,2,3,3}
In [91]: s1
Out[91]: {1, 2, 3}
In [92]: s2 = set([1,2,3,4,5,5])
In [93]: s2
Out[93]: {1, 2, 3, 4, 5}
In [94]: s3 = {}
## 错误的方式创建empty set
In [95]: s3
Out[95]: {}
In [96]: print type(s3)
<type 'dict'>
In [97]: print type(s1)
<type 'set'>
## 正确的创建empty set
In [98]: s4 = set()
In [99]: print type(s4)
<type 'set'>
change a set
Sets are mutable. But since they are unordered, indexing have no meaning.
We cannot access or change an element of set using indexing or slicing. Set does not support it.
We can add single element using the add() method and multiple elements using the update() method. The update() method can take tuples, lists, strings or other sets as its argument. In all cases, duplicates are avoided.
In [100]: my_set = {1,3,4}
In [101]: my_set
Out[101]: {1, 3, 4}
In [102]: my_set.add(2)
In [103]: my_set
Out[103]: {1, 2, 3, 4}
In [104]: my_set.update([4,5,6])
In [105]: my_set
Out[105]: {1, 2, 3, 4, 5, 6}
In [106]: my_set.update([4,5],{1,6,8},"abced",(100,200))
In [107]: my_set
Out[107]: {1, 2, 3, 4, 5, 6, 8, 100, 200, 'a', 'b', 'c', 'd', 'e'}
## set不支持按下标访问,不支持切片操作
In [108]: my_set[0] = 200
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-108-7f844179f8ad> in <module>()
----> 1 my_set[0] = 200
TypeError: 'set' object does not support item assignment
In [109]: my_set[1:3]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-109-ca619ada58f9> in <module>()
----> 1 my_set[1:3]
TypeError: 'set' object has no attribute '__getitem__'
remove elements from a set
A particular item can be removed from set using methods, discard() and remove().
The only difference between the two is that, while using discard() if the item does not exist in the set, it remains unchanged. But remove() will raise an error in such condition.
In [110]: my_set = {1,3,4,5,6}
In [111]: my_set
Out[111]: {1, 3, 4, 5, 6}
In [112]: my_set.discard(4)
In [113]: my_set
Out[113]: {1, 3, 5, 6}
In [114]: my_set.discard(6)
In [115]: my_set
Out[115]: {1, 3, 5}
In [116]: my_set.discard(2)
In [117]: my_set
Out[117]: {1, 3, 5}
## 使用remove删除一个不存在的元素时会报错,discard不会
In [118]: my_set.remove(2)
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-118-6c2f19c470bc> in <module>()
----> 1 my_set.remove(2)
KeyError: 2
Similarly, we can remove and return an item using the pop() method.
Set being unordered, there is no way of determining which item will be popped. It is completely arbitrary.
We can also remove all items from a set using clear().
In [119]: my_set = set("HelloWorld")
In [120]: my_set
Out[120]: {'H', 'W', 'd', 'e', 'l', 'o', 'r'}
In [121]: print my_set.pop()
e
In [122]: my_set
Out[122]: {'H', 'W', 'd', 'l', 'o', 'r'}
In [123]: my_set.clear()
In [124]: my_set
Out[124]: set()
Set Union
Union of A and B is a set of all elements from both sets.
Union is performed using | operator. Same can be accomplished using the method union().
In [125]: A = {1,2,3,4,5}
In [126]: B= {4,5,6,7,8}
In [127]: A | B
Out[127]: {1, 2, 3, 4, 5, 6, 7, 8}
In [128]: A.union(B)
Out[128]: {1, 2, 3, 4, 5, 6, 7, 8}
In [129]: B.union(A)
Out[129]: {1, 2, 3, 4, 5, 6, 7, 8}
Set Intersection
Intersection of A and B is a set of elements that are common in both sets.
Intersection is performed using & operator. Same can be accomplished using the method intersection().
In [125]: A = {1,2,3,4,5}
In [126]: B= {4,5,6,7,8}
In [130]: A & B
Out[130]: {4, 5}
In [131]: A.intersection(B)
Out[131]: {4, 5}
In [132]: B.intersection(A)
Out[132]: {4, 5}
Set Difference
Difference of A and B (A - B) is a set of elements that are only in A but not in B. Similarly, B - A is a set of element in B but not in A.
Difference is performed using - operator. Same can be accomplished using the method difference().
In [125]: A = {1,2,3,4,5}
In [126]: B= {4,5,6,7,8}
In [133]: A - B
Out[133]: {1, 2, 3}
In [134]: A.difference(B)
Out[134]: {1, 2, 3}
In [135]: B.difference(A)
Out[135]: {6, 7, 8}
In [136]: B - A
Out[136]: {6, 7, 8}
Set Symmetric Difference
Symmetric Difference of A and B is a set of elements in both A and B except those that are common in both.
Symmetric difference is performed using ^ operator. Same can be accomplished using the method symmetric_difference().
In [137]: A ^ B
Out[137]: {1, 2, 3, 6, 7, 8}
In [138]: A.symmetric_difference(B)
Out[138]: {1, 2, 3, 6, 7, 8}
In [139]: B.symmetric_difference(A)
Out[139]: {1, 2, 3, 6, 7, 8}
Frozenset
Frozenset is a new class that has the characteristics of a set, but its elements cannot be changed once assigned. While tuples are immutable lists, frozensets are immutable sets.
Sets being mutable are unhashable, so they can't be used as dictionary keys. On the other hand, frozensets are hashable and can be used as keys to a dictionary.
Frozensets can be created using the function frozenset().
This datatype supports methods like copy(), difference(), intersection(), isdisjoint(), issubset(), issuperset(), symmetric_difference() and union(). Being immutable it does not have method that add or remove elements.
In [140]: A = frozenset([1, 2, 3, 4])
In [141]: B = frozenset([3, 4, 5, 6])
In [142]: A.isdisjoint(B)
Out[142]: False
In [143]: A.difference(B)
Out[143]: frozenset({1, 2})
In [144]: A | B
Out[144]: frozenset({1, 2, 3, 4, 5, 6})
In [145]: A.add(3)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-145-7abb9af00bfc> in <module>()
----> 1 A.add(3)
AttributeError: 'frozenset' object has no attribute 'add'
intersection_update
In [158]: A = {1,2,3,4,5}
In [159]: B= {4,5,6,7,8}
In [179]: A & B
Out[179]: {4, 5}
In [160]: A.intersection_update(B)
In [161]: A
Out[161]: {4, 5}
In [162]: B
Out[162]: {4, 5, 6, 7, 8}
In [163]: A = {1,2,3,4,5}
In [164]: B.intersection_update(A)
In [165]: B
Out[165]: {4, 5}
In [166]: A
Out[166]: {1, 2, 3, 4, 5}
difference_update
In [177]: A = {1,2,3,4,5}
In [178]: B= {4,5,6,7,8}
In [180]: A - B
Out[180]: {1, 2, 3}
In [182]: A.difference_update(B)
In [183]: A
Out[183]: {1, 2, 3}
In [184]: B
Out[184]: {4, 5, 6, 7, 8}
In [185]: A = {1,2,3,4,5}
In [181]: B - A
Out[181]: {6, 7, 8}
In [186]: B.difference_update(A)
In [187]: B
Out[187]: {6, 7, 8}
In [188]: A
Out[188]: {1, 2, 3, 4, 5}
symmetric_difference_update
In [192]: A
Out[192]: {1, 2, 3, 4, 5}
In [193]: B
Out[193]: {4, 5, 6, 7, 8}
In [194]: A ^ B
Out[194]: {1, 2, 3, 6, 7, 8}
In [195]: A.symmetric_difference_update(B)
In [196]: A
Out[196]: {1, 2, 3, 6, 7, 8}
In [197]: B
Out[197]: {4, 5, 6, 7, 8}
In [198]: A = {1,2,3,4,5}
In [199]: B.symmetric_difference_update(A)
In [200]: B
Out[200]: {1, 2, 3, 6, 7, 8}
In [201]: A
Out[201]: {1, 2, 3, 4, 5}
注意事项
## 直接赋值
In [202]: s1 = {1,2,3,4,5}
In [203]: s1
Out[203]: {1, 2, 3, 4, 5}
In [204]: s2 = s1
In [205]: s2
Out[205]: {1, 2, 3, 4, 5}
In [206]: s2.add(8)
In [207]: s1
Out[207]: {1, 2, 3, 4, 5, 8}
##与list一样,当直接赋值给另外一个set,对任何一个对象作修改,变更都会传播
## 使用copy可以避免该问题
In [208]: s1 = {1,2,3,4,5}
In [209]: s1
Out[209]: {1, 2, 3, 4, 5}
In [210]: s2 = copy.copy(s1)
In [211]: s2
Out[211]: {1, 2, 3, 4, 5}
In [212]: s2.add(8)
In [213]: s2
Out[213]: {1, 2, 3, 4, 5, 8}
In [214]: s1
Out[214]: {1, 2, 3, 4, 5}