@nalan90
2017-07-28T09:30:30.000000Z
字数 7294
阅读 792
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]: s1Out[91]: {1, 2, 3}In [92]: s2 = set([1,2,3,4,5,5])In [93]: s2Out[93]: {1, 2, 3, 4, 5}In [94]: s3 = {}## 错误的方式创建empty setIn [95]: s3Out[95]: {}In [96]: print type(s3)<type 'dict'>In [97]: print type(s1)<type 'set'>## 正确的创建empty setIn [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_setOut[101]: {1, 3, 4}In [102]: my_set.add(2)In [103]: my_setOut[103]: {1, 2, 3, 4}In [104]: my_set.update([4,5,6])In [105]: my_setOut[105]: {1, 2, 3, 4, 5, 6}In [106]: my_set.update([4,5],{1,6,8},"abced",(100,200))In [107]: my_setOut[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] = 200TypeError: 'set' object does not support item assignmentIn [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_setOut[111]: {1, 3, 4, 5, 6}In [112]: my_set.discard(4)In [113]: my_setOut[113]: {1, 3, 5, 6}In [114]: my_set.discard(6)In [115]: my_setOut[115]: {1, 3, 5}In [116]: my_set.discard(2)In [117]: my_setOut[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_setOut[120]: {'H', 'W', 'd', 'e', 'l', 'o', 'r'}In [121]: print my_set.pop()eIn [122]: my_setOut[122]: {'H', 'W', 'd', 'l', 'o', 'r'}In [123]: my_set.clear()In [124]: my_setOut[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 | BOut[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 & BOut[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 - BOut[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 - AOut[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 ^ BOut[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]: FalseIn [143]: A.difference(B)Out[143]: frozenset({1, 2})In [144]: A | BOut[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 & BOut[179]: {4, 5}In [160]: A.intersection_update(B)In [161]: AOut[161]: {4, 5}In [162]: BOut[162]: {4, 5, 6, 7, 8}In [163]: A = {1,2,3,4,5}In [164]: B.intersection_update(A)In [165]: BOut[165]: {4, 5}In [166]: AOut[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 - BOut[180]: {1, 2, 3}In [182]: A.difference_update(B)In [183]: AOut[183]: {1, 2, 3}In [184]: BOut[184]: {4, 5, 6, 7, 8}In [185]: A = {1,2,3,4,5}In [181]: B - AOut[181]: {6, 7, 8}In [186]: B.difference_update(A)In [187]: BOut[187]: {6, 7, 8}In [188]: AOut[188]: {1, 2, 3, 4, 5}
symmetric_difference_update
In [192]: AOut[192]: {1, 2, 3, 4, 5}In [193]: BOut[193]: {4, 5, 6, 7, 8}In [194]: A ^ BOut[194]: {1, 2, 3, 6, 7, 8}In [195]: A.symmetric_difference_update(B)In [196]: AOut[196]: {1, 2, 3, 6, 7, 8}In [197]: BOut[197]: {4, 5, 6, 7, 8}In [198]: A = {1,2,3,4,5}In [199]: B.symmetric_difference_update(A)In [200]: BOut[200]: {1, 2, 3, 6, 7, 8}In [201]: AOut[201]: {1, 2, 3, 4, 5}
注意事项
## 直接赋值In [202]: s1 = {1,2,3,4,5}In [203]: s1Out[203]: {1, 2, 3, 4, 5}In [204]: s2 = s1In [205]: s2Out[205]: {1, 2, 3, 4, 5}In [206]: s2.add(8)In [207]: s1Out[207]: {1, 2, 3, 4, 5, 8}##与list一样,当直接赋值给另外一个set,对任何一个对象作修改,变更都会传播## 使用copy可以避免该问题In [208]: s1 = {1,2,3,4,5}In [209]: s1Out[209]: {1, 2, 3, 4, 5}In [210]: s2 = copy.copy(s1)In [211]: s2Out[211]: {1, 2, 3, 4, 5}In [212]: s2.add(8)In [213]: s2Out[213]: {1, 2, 3, 4, 5, 8}In [214]: s1Out[214]: {1, 2, 3, 4, 5}