7-python3 from beginner to combat-basic data type (dictionary-dictionary)

Source: Internet
Author: User

Python from the start to the actual combat series--the definition of a directory dictionary
    • A dictionary is another variable container model, and can store any type of object, with key-value (Key-value) storage, with extremely fast lookup speed;
      Each key value of the dictionary (key=>value) pairs with a colon (:) split, each pair is separated by a comma (,), and the entire dictionary is included in curly braces ({})

      语法格式:{ key1 : value1, key2 : value2, key3 : value3 ...}users={‘ 张三 ‘ : 18 , ‘ 李四 ‘ : 19 , ‘ 王五 ‘ : 20 , ‘ 赵六 ‘ : 19}
    • The key of the dictionary must be unique, and the data type of the value is immutable, but the value can be arbitrary or repetitive;

      # 编号作为键,键唯一,值可变users={ 1 :‘ 张三 ‘  , 2 :‘ 李四 ‘ , 3 :‘ 王五 ‘  , 4 :‘ 张三 ‘ }
Accessing values in the dictionary
    • Access the values in the dictionary using the dict[key]

      dict = {‘name‘ : ‘ SiberiaDante ‘ , ‘ age ‘ : 18  , ‘ address‘ : ‘ China ‘}print(dict[‘name‘]  # 结果 :SiberiaDante
Modify Dictionary
    • Modify the values in the dictionary according to the keys in the dictionary

      dict = {‘name‘ : ‘ SiberiaDante ‘ , ‘ age ‘ : 18  , ‘ address‘ : ‘ China ‘}print(dict[‘age‘])  # 输出:18dict [ ‘ age ‘ ] = 20print(dict[‘age‘])  # 输出: 20
    • Add a key-value pair to an existing dictionary

      dict = {‘name‘ : ‘SiberiaDante‘ , ‘age‘ : 18  , ‘address‘ : ‘China‘}print(dict) # 结果 :{‘name‘: ‘SiberiaDante‘, ‘age‘: 18, ‘address‘: ‘China‘}dict[‘language‘]=‘Python‘print(dict) # 结果 : {‘name‘: ‘SiberiaDante‘, ‘age‘: 18, ‘address‘: ‘China‘,‘language‘:‘Python‘}
Delete Dictionary
  • Delete a single element in the dictionary: Dict[key]

    dict = {‘name‘ : ‘SiberiaDante‘ , ‘age‘ : 18  , ‘address‘ : ‘China‘}print(dict) # 结果 : {‘name‘: ‘SiberiaDante‘, ‘age‘: 18, ‘address‘: ‘China‘}del dict[‘name‘]print(dict) # 结果 : { ‘age‘: 18, ‘address‘: ‘China‘}
  • Delete a dictionary: Del dict

    dict = {‘name‘ : ‘SiberiaDante‘ , ‘age‘ : 18  , ‘address‘ : ‘China‘}del dict
  • Empty dictionary: Dict.clear ()

       dict = {‘name‘ : ‘SiberiaDante‘ , ‘age‘ : 18  , ‘address‘ : ‘China‘}   print(dict) # 结果 : {‘name‘: ‘SiberiaDante‘, ‘age‘: 18, ‘address‘: ‘China‘}   dict.clear()    print(dict)  # 结果:{}
    Dictionary built-in functions & methods
  • Function

    len(dict)   计算字典元素个数,即键的总数。 str(dict)   输出字典,以可打印的字符串表示。    type(variable)  返回输入的变量类型,如果变量是字典就返回字典类型。
  • Method

    dict.clear() 删除字典内所有元素dict.copy()  返回一个字典的浅复制dict.fromkeys()  创建一个新字典,以序列seq中元素做字典的键,val为字典所有键对应的初始值dict.get(key, default=None)  返回指定键的值,如果值不在字典中返回default值key in dict 如果键在字典dict里返回true,否则返回falsedict.items() 以列表返回可遍历的(键, 值) 元组数组dict.keys()  以列表返回一个字典所有的键dict.setdefault(key, default=None)   和get()类似, 但如果键不存在于字典中,将会添加键并将值设为defaultdict.update(dict2)   把字典dict2的键/值对更新到dict里dict.values()    以列表返回字典中的所有值pop(key[,default])  删除字典给定键 key 所对应的值,返回值为被删除的值。key值必须给出。 否则,返回default值。popitem()   随机返回并删除字典中的一对键和值(一般删除末尾对)。
Dictionary and List comparison
    • Compared with list, Dict has the following features:
      • The speed of finding and inserting is very fast and will not slow with the increase of key;
        * Need to occupy a lot of memory, memory waste much;
    • Compared to dictionary, the list features:
      • The time to find and insert increases as the element increases;
      • Small footprint, little wasted memory;

7-python3 from beginner to combat-basic data type (dictionary-dictionary)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.