zoukankan      html  css  js  c++  java
  • python全栈开发从入门到放弃之字典的应用

    1、存值

    1 info_dic={'name':'egon','age':18,'sex':'male'}
    2 info_dic['job']='IT'            #根据key来存值
    3 print(info_dic)          
    4 
    5 输出结果:
    6 {'name': 'egon', 'age': 18, 'sex': 'male', 'job': 'IT'}

    2、取值

     1 1 info_dic={'name':'egon','age':18,'sex':'male'}
     2 2 print(info_dic['name'])     #根据Key来取值
     3 3 
     4 4 
     5 5 输出结果:
     6 6 egon
     7 #如果没有在字典中没有你要找的key则会报错
     8 info_dic={'name':'egon','age':18,'sex':'male'}
     9 print(info_dic['name11111111'])
    10 
    11 #get找不到要找的key值不会报错,会返回默认值None
    12 get()
    13 info_dic={'name':'egon','age':18,'sex':'male'}
    14 print(info_dic.get('name11'))
    15 None

    3、pop

    1 info_dic={'name':'egon','age':18,'sex':'male'}
    2 print(info_dic.pop('name111',None))     #不是单纯的删除,可以看到返回的值,如果没key存在则返回值,没有存在则返回默认值None
    3 
    4 输出结果:
    5 None

    4、popitem()

    1 info_dic={'name':'egon','age':18,'sex':'male'}
    2 print(info_dic.popitem())    #默认从后往前的删除取值,但不是单纯的删除有返回值
    3 print(info_dic)
    4 
    5 
    6 输出结果:
    7 ('sex', 'male')
    8 {'name': 'egon', 'age': 18}

    5、keys

    1 info_dic={'name':'egon','age':18,'sex':'male'}
    2 print(info_dic.keys())   #取字典的所有key
    3 
    4 
    5 输出结果:
    6 dict_keys(['name', 'age', 'sex'])

    6、values

    1 info_dic={'name':'egon','age':18,'sex':'male'}
    2 print(info_dic.values())     #取字典的所有value值
    3 
    4 
    5 输出结果:
    6 dict_values(['egon', 18, 'male'])

    7、键值对 items()

    info_dic={'name':'egon','age':18,'sex':'male'}
    print(info_dic.items())    #键值对,取字典的键值对
    
    输出结果:
    dict_items([('name', 'egon'), ('age', 18), ('sex', 'male')])

    应用场景:

    # print('========>')
    # for k in info_dic.keys():
    # print(k)

    # for val in info_dic.values():
    # print(val)

    # for k,v in info_dic.items(): #k,v=('name', 'egon')
    # print(k,v)

    8、长度 len

    1 info_dic={'name':'egon','age':18,'sex':'male'}
    2 print(len(info_dic))
    3 
    4 输出结果:
    5 3

    9、包含in

     1 #查看是否包含key、value、键值对有则返回True,没有则返回False
     2 info_dic={'name':'egon','age':18,'sex':'male'}
     3 print('name' in info_dic)
     4 print('name' in info_dic.keys())
     5 print('egon' in info_dic.values())
     6 print(('name','egon') in info_dic.items())
     7 
     8 
     9 输出结果:
    10 True
    11 True
    12 True
    13 True

    10、updata

    1 info_dic={'name':'egon','age':18,'sex':'male'}
    2 info_dic.update({'a':1,'name':'Egon'})     #对字典进行更新没有则往后添加,有的话则更改value是否改变,改变了则更新
    3 print(info_dic)
    4 
    5 
    6 
    7 输出结果:
    8 {'name': 'Egon', 'age': 18, 'sex': 'male', 'a': 1}

    11、

    别想一下造出大海,必须先由小河川开始。
  • 相关阅读:
    [LeetCode]2. Add Two Numbers链表相加
    Integration between Dynamics 365 and Dynamics 365 Finance and Operation
    向视图列添加自定义图标和提示信息 -- PowerApps / Dynamics365
    Update the Power Apps portals solution
    Migrate portal configuration
    Use variable to setup related components visible
    Loyalty management on Retail of Dynamic 365
    Modern Fluent UI controls in Power Apps
    Change screen size and orientation of a canvas app in Power App
    Communication Plan for Power Platform
  • 原文地址:https://www.cnblogs.com/zcfx/p/7256776.html
Copyright © 2011-2022 走看看