Python collection (set) type of operation "Go"

Source: Internet
Author: User
Tags iterable new set shallow copy

[Python]View Plaincopy
  1. Python's set is similar to other languages and is an unordered set of distinct elements, with basic functionality including relationship testing and de-duplication elements. The collection object also supports mathematical operations such as Union (union), intersection (intersection), Difference (poor), and sysmmetric difference (symmetric difference sets).
  2. Sets supports X in set, Len (set), and for X in set. As an unordered collection, sets does not record the element position or insertion point.  Therefore, sets does not support the operation of indexing, slicing, or other class sequences (Sequence-like).
  3. Here's a little simple example to illustrate the point.
  4. >>> x = set (' spam ')
  5. >>> y = set ([' h ',' a ',' m '])
  6. >>> x, y
  7. (Set ([' a ', ' P ', ' s ', ' m ']), set ([' a ', ' h ', ' m '] )
  8. Come back to the little apps.
  9. >>> x & y # intersection
  10. Set ([' a ', ' m '])
  11. >>> x | Y # and set
  12. Set ([' a ', ' P ', ' s ', ' h ', ' m '])
  13. >>> x-y # difference set
  14. Set ([' P ', ' s '])
  15. Remember the previous netizen asked how to remove the huge list of repeating elements, with a hash to solve the line, but the performance is not very high, with set to solve or is very good, examples are as follows:
  16. >>> a = [one, one, one, one, one, one]
  17. >>> B = Set (a)
  18. >>> b
  19. Set ([One, one, one, and one ])
  20. >>> C = [I for I in b]
  21. >>> C
  22. [One, one, one, and one ]
  23. It's cool, you can do it in a few lines.
  24. 1.8 Integrated
  25. The collection is used to contain a set of unordered objects. To create a collection, you can use the set () function and provide a series of items as follows:
  26. s = Set ([3,5,9,Ten]) #创建一个数值集合
  27. t = Set ("Hello") #创建一个唯一字符的集合
  28. Unlike lists and tuples, collections are unordered and cannot be indexed by numbers. Additionally, the elements in the collection cannot be duplicated. For example, if you examine the value of the T collection in the preceding code, the result would be:
  29. >>> T
  30. Set ([' H ', ' e ', ' l ', ' o '])
  31. Note that only one ' l ' appears.
  32. A collection supports a range of standard operations, including the set, intersection, difference set, and symmetric difference sets, such as:
  33. A = T | the set of S # T and S
  34. b = Intersection of T & S # T and S
  35. c = T–s # differential Set (item in T, but not in s)
  36. D = t ^ s # symmetric difference set (items in t or S, but not both)
  37. Basic operation:
  38. T.add (' x ') # Add an item
  39. S.update ([Ten,Panax Notoginseng]) # Add multiple items in S
  40. Use Remove () to delete an item:
  41. T.remove (' H ')
  42. Len (s)
  43. The length of the set
  44. X in s
  45. Test if X is a member of S
  46. X not in s
  47. Test if X is not a member of S
  48. S.issubset (t)
  49. S <= t
  50. Test if every element in S is in t
  51. S.issuperset (t)
  52. S >= t
  53. Tests if every element in T is in S
  54. S.union (t)
  55. s | T
  56. Returns a new set containing each element in S and T
  57. S.intersection (t)
  58. S & T
  59. Returns a new set containing the common elements in S and T
  60. S.difference (t)
  61. S-t
  62. Returns a new set containing elements in s but not in t
  63. S.symmetric_difference (t)
  64. s ^ t
  65. Returns a new set containing elements that are not duplicates in S and T
  66. S.copy ()
  67. Returns a shallow copy of the set "s"
  68. Note: Unions (), intersection (), difference (), and symmetric_difference () are non-operators (non-operator, which are shapes such as s.union (), will accept any Iterable as a parameter. Instead, their version of the operator (operator based counterparts) requires that the parameter must be sets. This avoids potential errors such as using Set (' abc ') & ' CBS ' instead of Set (' abc ') for more readable use . Intersection ('CBS '). From 2.3.   Changes made in version 1: All previous parameters must be sets.
  69. In addition, both set and Immutableset support the comparison between set and set. Two sets in this case it is equal only: the elements in each set are the elements in the other (the two are subset). A set is smaller than another set, only when the first set is the subset of the second set (it is a subset, but not equal). A set is hit by another set, only when the first set is the superset of the second set (it is a superset, but not equal).
  70. Sub-set and equality comparisons do not produce a complete sorting function. For example: Any two sets are not equal or sub-set, so the following operations will return False:a<b, a==b, or a>b.  Therefore, sets does not provide a __cmp__ method.
  71. Because sets only defines a partial sort function (subset relationship), the output of the List.sort () method is not defined for the list of sets.
  72. Operator
  73. Result of Operation
  74. Hash (s)
  75. Returns the hash value of s
  76. The following table lists the operations that are not available for Immutableset for Set two:
  77. Operator (Voperator)
  78. Equivalent to
  79. Result of Operation
  80. S.update (t)
  81. S |= t
  82. Returns the set "S" after the element in set "T" is added
  83. S.intersection_update (t)
  84. S &= t
  85. Returns only the set "s" containing the elements in set "T"
  86. S.difference_update (t)
  87. S-= t
  88. Returns the set "S" after deleting the element contained in set "T"
  89. S.symmetric_difference_update (t)
  90. S ^= t
  91. Returns a set "s" containing elements in set "T" or set "s", rather than both
  92. S.add (x)
  93. add element x to set "s"
  94. S.remove (x)
  95. remove element x from set "s" and throw keyerror if not present
  96. S.discard (x)
  97. If element x exists in set "s", delete
  98. S.pop ()
  99. Deletes and returns an indeterminate element in the set "s" and throws a keyerror if it is empty
  100. S.clear ()
  101. Delete all elements in set "s"
  102. Note: The non-operator version of Update (), Intersection_update (), Difference_update (), and Symmetric_difference_update () will accept any iterable as parameters. From 2.3.   Changes made in version 1: All previous parameters must be sets.
  103. Also note: This module also contains a union_update () method, which is an alias for the update () method. This method is included for backwards compatibility. Programmers should use the update () method more, because this method is also supported by the built-in set () and Frozenset () types.

Python collection (set) type of operation "Go"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.