Python study notes - the basics summary 02

01. dict

 {} Indicates that this is a dict, is a key: value set, the corresponding value can be found by looking for the key, may be calculated by Len Dict set size function ();
 d = {
//key:value 'Adam': 95, 'Lisa': 85, 'Bart': 59 }
 You may be used in the form of d [key] to find the corresponding value, except that the list, the index must be used to return the corresponding list element, and using dict key:

     Note: Access dict of value by key, as long as the key exists, dict returns the corresponding value. If the key does not exist, will direct error: KeyError. So there are two ways to avoid KeyError occur:

     . A first, first determine what the key is present, use the in operator:

 if 'Paul' in d:
     print d['Paul']

     If 'Paul' does not exist, if the statement is determined False, naturally not execute print d [ 'Paul'], thus avoiding errors.

     . B get method is to use a two dict itself provides, when the Key does not exist, the process returns None:

 >>> print d.get('Bart') 

02. dict characteristics (disordered, key non-repeating)

     a. Find speed, regardless dict has 10 elements or 100,000 elements, the search speed are the same. The list of search speed gradually decreased with the increase of the elements. But dict fast search speed is not without cost, the disadvantage is that dict large memory footprint, but also waste a lot of contents, list the contrary, small memory footprint, but look slow.

         Ps. Since dict is Find by key, therefore, in a dict in, key can not be repeated.

     b. stored key-value pair is not ordered sequence! This list is not the same:

     c. As a key element to be immutable, Python basic types such as strings, integers, floating point numbers are immutable, it can be used as key. But the list is variable, it can not serve as key.

03. dict update

      Add a new key-value: d [ 'Paul'] = 72  

          . Ps If the key value already exists, the existing value is updated value operation;

04. dict traversal key

      Ps. May be acquired by the corresponding value d [key]

      for key in d:

            print key

05. Set (unordered not repeat)

      Set of elements similar to List, is present in the form of a collection

      d=set(['A','B','C','C'])

      print d      >>> set(['A','C','B'])

06. Set access

     Because it is stored unordered collection set, so we can not be accessed by indexing, but we can use in operator judgment:

 >>> 'A' in s
 True

07. Set Features

       a.  internal structure and the like dict set, the only difference is not stored value, thus determining whether an element in the set quickly.

       Similar elements of B and dict. Set storage key, the object must be constant, therefore, the object can not be placed in any of the variable in the set.

       Element c. Set is not stored in the order.

08. Set traversal

       for val in s:

      print val

09. Set Update

  Adding a new element: s.add () // there is no error will elements 
remove elements: s.remove ()
// element does not exist will complain

Guess you like

Origin www.cnblogs.com/sccd/p/10141101.html