Python中的集合set

  1 >>> help(set)
  2 Help on class set in module __builtin__:
  3 
  4 class set(object)
  5  |  set(iterable) --> set object
  6  |
  7  |  Build an unordered collection of unique elements.#无序、独一无二的元素
  8  |
  9  |  Methods defined here:
 10  |
 11  |  __and__(...)
 12  |      x.__and__(y) <==> x&y
 13  |
 14  |  __cmp__(...)
 15  |      x.__cmp__(y) <==> cmp(x,y)
 16  |
 17  |  __contains__(...)
 18  |      x.__contains__(y) <==> y in x.
 19  |
 20  |  __eq__(...)
 21  |      x.__eq__(y) <==> x==y
 22  |
 23  |  __ge__(...)
 24  |      x.__ge__(y) <==> x>=y
 25  |
 26  |  __getattribute__(...)
 27  |      x.__getattribute__('name') <==> x.name
 28  |
 29  |  __gt__(...)
 30  |      x.__gt__(y) <==> x>y
 31  |
 32  |  __iand__(...)
 33  |      x.__iand__(y) <==> x&y
 34  |
 35  |  __init__(...)
 36  |      x.__init__(...) initializes x; see x.__class__.__doc__ for signature
 37  |
 38  |  __ior__(...)
 39  |      x.__ior__(y) <==> x|y
 40  |
 41  |  __isub__(...)
 42  |      x.__isub__(y) <==> x-y
 43  |
 44  |  __iter__(...)
 45  |      x.__iter__() <==> iter(x)
 46  |
 47  |  __ixor__(...)
 48  |      x.__ixor__(y) <==> x^y
 49  |
 50  |  __le__(...)
 51  |      x.__le__(y) <==> x<=y
 52  |
 53  |  __len__(...)
 54  |      x.__len__() <==> len(x)
 55  |
 56  |  __lt__(...)
 57  |      x.__lt__(y) <==> x<y
 58  |
 59  |  __ne__(...)
 60  |      x.__ne__(y) <==> x!=y
 61  |
 62  |  __or__(...)
 63  |      x.__or__(y) <==> x|y
 64  |
 65  |  __rand__(...)
 66  |      x.__rand__(y) <==> y&x
 67  |
 68  |  __reduce__(...)
 69  |      Return state information for pickling.
 70  |
 71  |  __repr__(...)
 72  |      x.__repr__() <==> repr(x)
 73  |
 74  |  __ror__(...)
 75  |      x.__ror__(y) <==> y|x
 76  |
 77  |  __rsub__(...)
 78  |      x.__rsub__(y) <==> y-x
 79  |
 80  |  __rxor__(...)
 81  |      x.__rxor__(y) <==> y^x
 82  |
 83  |  __sizeof__(...)
 84  |      S.__sizeof__() -> size of S in memory, in bytes
 85  |
 86  |  __sub__(...)
 87  |      x.__sub__(y) <==> x-y
 88  |
 89  |  __xor__(...)
 90  |      x.__xor__(y) <==> x^y
 91  |
 92  |  add(...)
 93  |      Add an element to a set.
 94  |
 95  |      This has no effect if the element is already present.
 96  |
 97  |  clear(...)
 98  |      Remove all elements from this set.
 99  |
100  |  copy(...)
101  |      Return a shallow copy of a set.
102  |
103  |  difference(...)
104  |      Return the difference of two or more sets as a new set.
105  |
106  |      (i.e. all elements that are in this set but not the others.)
107  |
108  |  difference_update(...)
109  |      Remove all elements of another set from this set.
110  |
111  |  discard(...)
112  |      Remove an element from a set if it is a member.
113  |
114  |      If the element is not a member, do nothing.
115  |
116  |  intersection(...)
117  |      Return the intersection of two sets as a new set.
118  |
119  |      (i.e. all elements that are in both sets.)
120  |
121  |  intersection_update(...)
122  |      Update a set with the intersection of itself and another.
123  |
124  |  isdisjoint(...)
125  |      Return True if two sets have a null intersection.
126  |
127  |  issubset(...)
128  |      Report whether another set contains this set.
129  |
130  |  issuperset(...)
131  |      Report whether this set contains another set.
132  |
133  |  pop(...)
134  |      Remove and return an arbitrary set element.
135  |      Raises KeyError if the set is empty.
136  |
137  |  remove(...)
138  |      Remove an element from a set; it must be a member.
139  |
140  |      If the element is not a member, raise a KeyError.
141  |
142  |  symmetric_difference(...)
143  |      Return the symmetric difference of two sets as a new set.
144  |
145  |      (i.e. all elements that are in exactly one of the sets.)
146  |
147  |  symmetric_difference_update(...)
148  |      Update a set with the symmetric difference of itself and another.
149  |
150  |  union(...)
151  |      Return the union of sets as a new set.
152  |
153  |      (i.e. all elements that are in either set.)
154  |
155  |  update(...)
156  |      Update a set with the union of itself and others.
157  |
158  |  ----------------------------------------------------------------------
159  |  Data and other attributes defined here:
160  |
161  |  __hash__ = None
162  |
163  |  __new__ = <built-in method __new__ of type object at 0x1E1CD668>
164  |      T.__new__(S, ...) -> a new object with type S, a subtype of T
通过下面例子,可以看出:集合是非重复的,集合是无序的。
1 >>> d=set('chooses')
2 >>> d
3 set(['h', 'c', 'e', 's', 'o'])
4 >>> d
5 set(['h', 'c', 'e', 's', 'o'])
6 >>> d
7 set(['h', 'c', 'e', 's', 'o'])
8 >>>

如果需要有序的集合,可以改为list:

 1 >>> d=set('chooses')
 2 >>> d
 3 set(['h', 'c', 'e', 's', 'o'])
 4 >>> d
 5 set(['h', 'c', 'e', 's', 'o'])
 6 >>> d
 7 set(['h', 'c', 'e', 's', 'o'])
 8 >>> mylist=list(d)
 9 >>> mylist
10 ['h', 'c', 'e', 's', 'o']
11 >>> mylist.sort()
12 >>> mylist
13 ['c', 'e', 'h', 'o', 's']
14 >>>
原文地址:https://www.cnblogs.com/cdsj/p/3538525.html