python notice3

Dictionary: dictionary (dict) python is the only type of a map he is enclosed in {} key is unique to the composition in dict when saving the key, key calculated according to a memory address.... the key-value is then stored in the address. such an algorithm is called a hash algorithm, therefore, remember, the key is stored in the dict key-value must be a hash, while in a dictionary unique key . Hash currently available data types: int, str, tuple, bool. Not hash data types: list, dict, set.

  Syntax: {key1: value1, key2: value2 ....} Note: The key-value pair is disordered, so that the operation can not be sliced.

1. increase

  dict [key] = value if the key does not exist, a new key-value pair; if key is present, it will replace the corresponding value.

  dict.setdefault (key, value) if the key does not exist, add a key-value pair; if the key exists, setdefault not work.

2. deleted

  dict.pop (key) to delete the specified key-value, and returns the corresponding value

  del dict [key] to delete the specified key-value

  dict.popitem () random delete key-value, while the return key-value.

  dict.clear () to empty the contents of the dictionary

3. change

  dict [existing key] = new value,

  d1.update (d2) the contents d2 d1 is updated to the more. If the same name key. Alternatively the modification. If key does not exist, is new.

4. Charles

  dict [key] can direct the investigation, if the key does not exist error.

  dict.get (key) to check directly, if the key does not exist, returns None, or dict.get (key, return value), the set return value.

  dict.setdefault (key, value) if the key does not exist, adding a key-value pair, and returns the corresponding value; if key is present, the return value inherent.

5. Other related operations

  dict.keys () to obtain all the dictionary key, then there is a high copy list may be used for loop through

  dict.values ​​() to obtain all the dictionary value, then there is a high copy list

  dict.items () Gets all key-value pairs to the dictionary, and then a list exists in the form of imitation tuple

    More convenient a, b = [1, 2] cycle through other tuples; deconstruction \ unpack a, b = 1, 2; a, b = (1, 2)

6. nesting dictionary

 eg:dic1 = {
    "name": "小明",
    "age": 28,
    "wife": {
      "name": '小红',
      "age": 28
       },
    "children": ['第⼀个毛孩', '第⼆个⽑孩'],
      }
  print(dic1.get("wife").get("name"))

  print(dic1.get("children"))
  print(dic1.get("children")[1])

 7 difference between == and is the

  == comparison value is equal on both sides of the variable

  is the memory address of the variable is equal on both sides of the comparison Note: The address query variable print (id (variable))

  Supplement is used when the address:   

    # Numeric data pool range -5 to 256, when the numbers within this range, the address is equal on both sides of IS

    # If there are special characters in the string of their memory address is not the same

    # * String as a single memory address them within 20, 21 or more inconsistent single memory address *

  NOTE: The only difference between the two can be compared only when the terminal performs out, PyCharm not, all of the same file, a py strings are generally used as the memory address.

The coding and decoding

  encode (encoding) ---- get the corresponding encoded bytes of plaintext

  decode (decoding method) ----- The encoded bytes decoded to the plain text

  Note: What is necessary to encode what is decoded, otherwise it will be garbled.

  supplement:

    python3 in running stage using Unicode, you can display all content. pycharm when stored default utf-8 type.

    Data transfer and storage type bytes are used.

  Coding knowledge:
         ASCII code:
               does not support Chinese
               support alphanumeric symbols
               8 byte
         gbk code GB:
               supports Chinese, English, numbers, symbols
               English 16 two-byte
               Chinese 16 two-byte
         unicode Unicode
                support for Chinese, English, numbers, symbols,
                English four-byte 32-bit
                Chinese four 32-bit byte
         utf-8 Unicode variable length with a minimum 8
                English 8 byte
                Chinese three bytes 24

  

 

Guess you like

Origin www.cnblogs.com/xiaolu-fan/p/11150853.html