Class python container - collection (set) and the deque (the deque)

  In Python, in addition to the list (List), tuple (tuple), the dictionary (dict) and other common container type, are also provided set (SET), deque (the deque) and other data types.

  set:

    1, the order of addition elements can not be recorded.

    2, the elements can not be repeated. (You can use this as a list of elements to weight)

    3, the container is variable, the elements may be changed in the container.

    4, there is an immutable version - frozenset.

  set of built-in methods:

1 >>> [e for e in dir(set) if not e.startswith('_')]
2 ['add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 

'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove',

'symmetric_difference', 'symmetric_difference_update', 'union', 'update'] 3 >>>

   Method set:

  <=, Equivalent to calling the issubset () method, determines whether the subset is set in front of the rear set.

  > =, Equivalent to calling the issuperset () method, it is determined whether or not a superset of the set of the front of the rear set.

  -, equivalent to calling the difference () method, by subtracting the latter in front of the collection set.

  &, Equivalent to calling the intersection method for obtaining intersection of two sets.

  ^, Calculates the exclusive OR of the two sets of results, and is set with two sets of elements by subtracting the intersection.

= {. 1 >>> set_a }
 >>> set_a.add (2 ) # additional element is set
 >>> SET_A
{ . 1, 2 }
 >>> set_a.remove (. 1 ) # remove elements in the set, but when removing an element not present in the collection being given 
>>> SET_A
{ 2 }
 >>> set_a.remove (. 1 ) # when removing an element not present in the collection being given
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 1
>>> set_a.add(1)
>>> set_a
{ . 1, 2 }
 >>> set_a.discard (. 1 ) # remove elements in the set, when removing an element not present in the collection no error
 >>> SET_A
{ 2 }
 >>> set_a.discard (. 1 ) # Upon removal element in the set does not exist error
 >>> SET_A
{ 2 }
 >>> set_b = SET () # set to use a constructor to create collections
 >>> set_b.add (. 1 )
 >>> set_b.add (2 )
 >>> set_b.add (. 3 )
 >>> SET_A. issubset (set_b) # determines whether to set a subset of the set of b
True
>>> set_a.issuperset (set_b) # determines whether the set is a superset of the set of b
False
Set_c = >>> set_b.difference (SET_A) # subtracting element in the set with a set of b, to obtain a new set of C
 >>> set_c
{ . 1,. 3 }
 >>> set_b.difference_update (SET_A) # subtracting element in the set with a set of b, change set B
 >>> set_b
{1, 3}
>>>
>>> set_c.clear () # Clear the set of elements in c
 >>> set_c
set()
>>> set_b.add(2)
>>> set_b
{1, 2, 3}
>>> set_a
{ 2 }
 >>> set_c = set_b.intersection (SET_A) # intersection of two sets is calculated to obtain a new set of C
 >>> set_c
{ 2 }
 >>> set_b.intersection_update (SET_A) # calculate the intersection of two sets of change set B
 >>> set_b
{2}

>>> set_b.add(1) >>> set_b.add(3) >>> set_a {2} >>> set_b { . 1, 2,. 3 } >>> set_a.add (. 4 ) >>> set_c = set_a.union (set_b) # union of two sets is calculated to obtain a new set >>> set_c { . 1, 2,. 3,. 4 } >>> set_a.update (set_b) # calculates two sets of set and change the set A >>> SET_A {1, 2, 3, 4}
>>> set_a {1, 2, 3, 4} >>> set_b { . 1, 2,. 3 } >>> set_a.isdisjoint (set_b) # determines whether the two sets contain the same elements, if not returns True, otherwise return False. False >>> set_b.isdisjoint(set_b) False >>> set_b.isdisjoint(set_a) False >>> set_a {1, 2, 3, 4} >>> set_b { . 1, 2,. 3 } >>> set_a.symmetric_difference (set_b) # Returns the two collection elements of the set do not overlap. { . 4 } >>> set_a.symmetric_difference_update (set_b) # remove additional current collection in a specified set of the same elements, and another set of different elements inserted into the current collection specified >>> SET_A {4} >>>

 

  Double-ended queue (deque):

    

Guess you like

Origin www.cnblogs.com/xuxianshen/p/12384316.html