python learning the third week summary

A, Python set of collection

1, set concept

  • variable is set, disordered, collection of elements will not be repeated. Elements and their elements inside the set of non-hash type can not occur. (I.e. set of elements required can be hash)
  • Common types of non-hash list, set, bytearray
  • set of elements that are not indexed, but can iterate

2, set the definition

  • For example: s1 = set ()
  • For example: s2 = set (range (5))
  • For example: s3 = { 'a', 2,2,3, 'abc'} is equivalent to the set = { 'a', 2,3, 'abc'}. set inside the elements can not be repeated.

3, set common operations

  • add (elem) Returns the value None # Note that only once add an element, if the element is present, not added
  • update (* others) # combined with other elements to the set to set, others must be iterable. Return value None situ modification
  • remove (elem) is removed from the set of an element, the element does not exist exception is thrown keyError
  • deiscard (elem) removes an element from the set, the element does not exist to do nothing.
  • pop () -> item removes an element from the collection, and returns the random elements to be removed. Abnormal call returns an empty collection keyError
  • clear () Removes all elements
  • set is a collection of types, elements unique, non-repairable. The so-called modification is "the current element into a completely different elements, adding a new element is to remove"
  • set set of non-linear structure, not index

4, collection

  • Complete Works :( union U, a collection of union mean) of all elements.
  • Subsets: (subset and a subset of a superset super) a set A of all other elements in the set B, A is a subset of B, B is superset of A
  • And sets: a set of a plurality of combined results
  • Intersection: more public part of the collection
  • Set difference: the collection and removal of other portions of the set of public
  • Subset and superset true: A is a subset of B and A is not equal to B, A is the subset B, B is superset of the set A

5, set operators

  • And set operators: |
    • [|] And set operators (with a union (* others) can also be) (returns a new collection)
    • [| =] Is equivalent to the update method. And multiple collections and in situ modification
    • And Set: The set of all the elements of two A and B are merged together
  • Intersection operator: &
    • [&] Is equivalent to the intersection of a plurality of sets of returns intersection. (Returns new collection)
    • [] = & Intersection_update equivalent to the intersection and a plurality of sets of acquisition, and in-place modification.
    • Intersection: set A and B, of the elements belonging to the set B consisting of
  • Set difference operator: -
    • [-] is equivalent difference (* others) to return the difference between the set and a plurality of sets, it will return a new set of
    • [-] = equivalents difference_update (* others) acquiring a plurality of sets of and difference in situ modification and
    • Difference sets: set A and B, A does not belong to the genus of all elements of the set consisting of B
  • Symmetric difference: ^
    • ^ [Identical to] symmetric_differece (other) returns a new set (set and another set difference)

 

Two, Python dictionary in the dict

1, dict dictionary concept

  • A dictionary is a nonlinear structure, the key is composed of key-value pairs of data sets.
  • Dictionary features: variable, disorderly, key non-repetition.

2, dict dictionary definition

  • Dict} or {[()] can be defined an empty dictionary

  • [Dict (** kwargs)] may be used key-name = value pairs to initialize a dictionary

 

 

  •  [Dict (iterable, ** kwarg)] using iterables configuration and dictionaries of name = value, but iterables element must be a dual structure

 

 

  •  [Dict (mapping, ** kwarg)] using a dictionary to build another dictionary

 

 

  • {} Directly defined: for example: e = { "a": 10, "b": 20, "c": 30}
  • fromkeys (iterable, value) [Method # class initialization set to build a dictionary in accordance with
  1. iterable: a set of keys that can be iterative
  2. value: Default is the default for all corresponding built. (I can not write if there is no default None)

 

 

 3, element access

  • [D [key]] Return value corresponding available value, if there may be no exception is thrown keyError
  • [Get (key [, default])] Return value corresponding key value, if the key does not exist, returns the default value of default, default values ​​for the missing None
  • [SetDefault (key [, default])] Return key value corresponding to the value, if the key does not exist, add a pair of kv, value set to default, and return to default, if the default is not set, None is returned
  • [Keys ()] Gets a dictionary of collection of all the key
  • [Values ​​()}, or the set of all the values ​​in the dictionary of values
  • [Items ()] Gets a collection of all the dictionary consisting of key-value pairs

4, dict dictionary common operations

(1) New and modified

  • [D [key] = value] The modified value key object is value, which if the key does not exist corresponding to the new key value added
  • [Update ([other]) -> None] a use of the dictionary data dictionary is updated, if the key does not exist is added, covering the key corresponding to the key exists on the already existing value. Place modification, no return value

(2) deleted

  • [Pop (key [, default])] corresponding to the key value is removed, and returns the corresponding value of the key. If the key does not exist to return the default value defalut. If the default is not set, and the key does not exist will throw an exception keyError
  • [Popitem ()] Removes and returns an arbitrary key-value pairs, Dictionary throws an exception when empty empty KeyError
  • [Clear ()] emptied dictionary
  • [Del d [key]] Deletes the specified key value pairs

(3) traversal

  • list (d.keys ()) according to a set of dictionary building set or corresponding list
  1. Note: [for i in a.keys ()] is equivalent to [] for i in a direct or directly using a method of traversing an element tuple
    [for i, k in a.items ()]

Guess you like

Origin www.cnblogs.com/fjjj/p/12626529.html