[关闭]
@nalan90 2017-07-28T17:30:30.000000Z 字数 7294 阅读 592

专题二 set常用操作

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.

  1. In [90]: s1 = {1,2,3,3}
  2. In [91]: s1
  3. Out[91]: {1, 2, 3}
  4. In [92]: s2 = set([1,2,3,4,5,5])
  5. In [93]: s2
  6. Out[93]: {1, 2, 3, 4, 5}
  7. In [94]: s3 = {}
  8. ## 错误的方式创建empty set
  9. In [95]: s3
  10. Out[95]: {}
  11. In [96]: print type(s3)
  12. <type 'dict'>
  13. In [97]: print type(s1)
  14. <type 'set'>
  15. ## 正确的创建empty set
  16. In [98]: s4 = set()
  17. In [99]: print type(s4)
  18. <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.

  1. In [100]: my_set = {1,3,4}
  2. In [101]: my_set
  3. Out[101]: {1, 3, 4}
  4. In [102]: my_set.add(2)
  5. In [103]: my_set
  6. Out[103]: {1, 2, 3, 4}
  7. In [104]: my_set.update([4,5,6])
  8. In [105]: my_set
  9. Out[105]: {1, 2, 3, 4, 5, 6}
  10. In [106]: my_set.update([4,5],{1,6,8},"abced",(100,200))
  11. In [107]: my_set
  12. Out[107]: {1, 2, 3, 4, 5, 6, 8, 100, 200, 'a', 'b', 'c', 'd', 'e'}
  13. ## set不支持按下标访问,不支持切片操作
  14. In [108]: my_set[0] = 200
  15. ---------------------------------------------------------------------------
  16. TypeError Traceback (most recent call last)
  17. <ipython-input-108-7f844179f8ad> in <module>()
  18. ----> 1 my_set[0] = 200
  19. TypeError: 'set' object does not support item assignment
  20. In [109]: my_set[1:3]
  21. ---------------------------------------------------------------------------
  22. TypeError Traceback (most recent call last)
  23. <ipython-input-109-ca619ada58f9> in <module>()
  24. ----> 1 my_set[1:3]
  25. 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.

  1. In [110]: my_set = {1,3,4,5,6}
  2. In [111]: my_set
  3. Out[111]: {1, 3, 4, 5, 6}
  4. In [112]: my_set.discard(4)
  5. In [113]: my_set
  6. Out[113]: {1, 3, 5, 6}
  7. In [114]: my_set.discard(6)
  8. In [115]: my_set
  9. Out[115]: {1, 3, 5}
  10. In [116]: my_set.discard(2)
  11. In [117]: my_set
  12. Out[117]: {1, 3, 5}
  13. ## 使用remove删除一个不存在的元素时会报错,discard不会
  14. In [118]: my_set.remove(2)
  15. ---------------------------------------------------------------------------
  16. KeyError Traceback (most recent call last)
  17. <ipython-input-118-6c2f19c470bc> in <module>()
  18. ----> 1 my_set.remove(2)
  19. 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().

  1. In [119]: my_set = set("HelloWorld")
  2. In [120]: my_set
  3. Out[120]: {'H', 'W', 'd', 'e', 'l', 'o', 'r'}
  4. In [121]: print my_set.pop()
  5. e
  6. In [122]: my_set
  7. Out[122]: {'H', 'W', 'd', 'l', 'o', 'r'}
  8. In [123]: my_set.clear()
  9. In [124]: my_set
  10. Out[124]: set()

Set Union

image_1bm1k6jo71sgr1jcs12ggcev13ed9.png-30.8kB

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().

  1. In [125]: A = {1,2,3,4,5}
  2. In [126]: B= {4,5,6,7,8}
  3. In [127]: A | B
  4. Out[127]: {1, 2, 3, 4, 5, 6, 7, 8}
  5. In [128]: A.union(B)
  6. Out[128]: {1, 2, 3, 4, 5, 6, 7, 8}
  7. In [129]: B.union(A)
  8. Out[129]: {1, 2, 3, 4, 5, 6, 7, 8}

Set Intersection

image_1bm1k9f5g7vd16j712o919665qkm.png-32.2kB

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().

  1. In [125]: A = {1,2,3,4,5}
  2. In [126]: B= {4,5,6,7,8}
  3. In [130]: A & B
  4. Out[130]: {4, 5}
  5. In [131]: A.intersection(B)
  6. Out[131]: {4, 5}
  7. In [132]: B.intersection(A)
  8. Out[132]: {4, 5}

Set Difference

image_1bm1kcqee7gje562g813o66qm13.png-27.9kB

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().

  1. In [125]: A = {1,2,3,4,5}
  2. In [126]: B= {4,5,6,7,8}
  3. In [133]: A - B
  4. Out[133]: {1, 2, 3}
  5. In [134]: A.difference(B)
  6. Out[134]: {1, 2, 3}
  7. In [135]: B.difference(A)
  8. Out[135]: {6, 7, 8}
  9. In [136]: B - A
  10. Out[136]: {6, 7, 8}

Set Symmetric Difference

image_1bm1ki1uuuqga8q1poobgck5s1g.png-30.1kB

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().

  1. In [137]: A ^ B
  2. Out[137]: {1, 2, 3, 6, 7, 8}
  3. In [138]: A.symmetric_difference(B)
  4. Out[138]: {1, 2, 3, 6, 7, 8}
  5. In [139]: B.symmetric_difference(A)
  6. 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.

  1. In [140]: A = frozenset([1, 2, 3, 4])
  2. In [141]: B = frozenset([3, 4, 5, 6])
  3. In [142]: A.isdisjoint(B)
  4. Out[142]: False
  5. In [143]: A.difference(B)
  6. Out[143]: frozenset({1, 2})
  7. In [144]: A | B
  8. Out[144]: frozenset({1, 2, 3, 4, 5, 6})
  9. In [145]: A.add(3)
  10. ---------------------------------------------------------------------------
  11. AttributeError Traceback (most recent call last)
  12. <ipython-input-145-7abb9af00bfc> in <module>()
  13. ----> 1 A.add(3)
  14. AttributeError: 'frozenset' object has no attribute 'add'

intersection_update
  1. In [158]: A = {1,2,3,4,5}
  2. In [159]: B= {4,5,6,7,8}
  3. In [179]: A & B
  4. Out[179]: {4, 5}
  5. In [160]: A.intersection_update(B)
  6. In [161]: A
  7. Out[161]: {4, 5}
  8. In [162]: B
  9. Out[162]: {4, 5, 6, 7, 8}
  10. In [163]: A = {1,2,3,4,5}
  11. In [164]: B.intersection_update(A)
  12. In [165]: B
  13. Out[165]: {4, 5}
  14. In [166]: A
  15. Out[166]: {1, 2, 3, 4, 5}

difference_update
  1. In [177]: A = {1,2,3,4,5}
  2. In [178]: B= {4,5,6,7,8}
  3. In [180]: A - B
  4. Out[180]: {1, 2, 3}
  5. In [182]: A.difference_update(B)
  6. In [183]: A
  7. Out[183]: {1, 2, 3}
  8. In [184]: B
  9. Out[184]: {4, 5, 6, 7, 8}
  10. In [185]: A = {1,2,3,4,5}
  11. In [181]: B - A
  12. Out[181]: {6, 7, 8}
  13. In [186]: B.difference_update(A)
  14. In [187]: B
  15. Out[187]: {6, 7, 8}
  16. In [188]: A
  17. Out[188]: {1, 2, 3, 4, 5}

symmetric_difference_update
  1. In [192]: A
  2. Out[192]: {1, 2, 3, 4, 5}
  3. In [193]: B
  4. Out[193]: {4, 5, 6, 7, 8}
  5. In [194]: A ^ B
  6. Out[194]: {1, 2, 3, 6, 7, 8}
  7. In [195]: A.symmetric_difference_update(B)
  8. In [196]: A
  9. Out[196]: {1, 2, 3, 6, 7, 8}
  10. In [197]: B
  11. Out[197]: {4, 5, 6, 7, 8}
  12. In [198]: A = {1,2,3,4,5}
  13. In [199]: B.symmetric_difference_update(A)
  14. In [200]: B
  15. Out[200]: {1, 2, 3, 6, 7, 8}
  16. In [201]: A
  17. Out[201]: {1, 2, 3, 4, 5}

注意事项

  1. ## 直接赋值
  2. In [202]: s1 = {1,2,3,4,5}
  3. In [203]: s1
  4. Out[203]: {1, 2, 3, 4, 5}
  5. In [204]: s2 = s1
  6. In [205]: s2
  7. Out[205]: {1, 2, 3, 4, 5}
  8. In [206]: s2.add(8)
  9. In [207]: s1
  10. Out[207]: {1, 2, 3, 4, 5, 8}
  11. ##与list一样,当直接赋值给另外一个set,对任何一个对象作修改,变更都会传播
  12. ## 使用copy可以避免该问题
  13. In [208]: s1 = {1,2,3,4,5}
  14. In [209]: s1
  15. Out[209]: {1, 2, 3, 4, 5}
  16. In [210]: s2 = copy.copy(s1)
  17. In [211]: s2
  18. Out[211]: {1, 2, 3, 4, 5}
  19. In [212]: s2.add(8)
  20. In [213]: s2
  21. Out[213]: {1, 2, 3, 4, 5, 8}
  22. In [214]: s1
  23. Out[214]: {1, 2, 3, 4, 5}
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注