• 字典


    #字典是没有顺序的

     1 info = {
     2     'name':'jesse',
     3     'age':'21',
     4     'job':'it'
     5 }
     6 
     7 b = {
     8     'name':'tt',
     9     'name_2':'djj',
    10     'money':1000
    11 }

    #合并两个字典

    info.update(b)
    print(info)

    #将字典变为元组

    print(info.items())

    #dict.fromkeys(seq[, value])
    #seq -- 字典键值列表。
    #value -- 可选参数, 设置键序列(seq)的值。
    #所有的多层共享一个内存地址

    test = info.fromkeys([6,7,8],['tt','o'])
    print(test)

    #查找

    1 info['name']
    2 info.get('name') #建议这个方法
    3 'name' in info   #如果有返回True,否则返回fasle

    #增加

    info['address']='cd'

    #字典删除

    1 info.pop('age') #标准删除
    2 del info['name']
    3 info.popitem() #随机删除

    #多级字典嵌套及操作

    city = {
        'sicuan':{'chengdu':['wuhouqu','jinjiangqu']},
        'guangdong':{'guangzhou':['huaduqu','conghuaqu']}
    }
    print(city['sicuan']['chengdu'][0])

    #打印所有的值

    print(city.values())

    #打印所有的key

    print(city.keys())

    #嵌套增加格式要对齐,如果字典中包含有给定键,则返回该键对应的值,否则返回为该键设置的值

    city.setdefault('xiaochi',{'shuijiao':['lade','tiande']})

    #字典的循环

    1 for i in city:
    2     print(i,city[i]) #建议用这个循环
    3 for k,v in city.items():
    4     print(k,v)

     

  • 相关阅读:
    whatweb tree
    testUrl
    ParseUrl
    whatweb wordpress.rb
    LeetCode: Binary Tree Level Order Traversal 解题报告
    LeetCode: Minimum Path Sum 解题报告
    Lintcode: Sort Colors II 解题报告
    LeetCode: Validate Binary Search Tree 解题报告
    LeetCode: Longest Common Prefix 解题报告
    LeetCode: Maximum Subarray 解题报告
  • 原文地址:https://www.cnblogs.com/jesse-gong/p/7652123.html
Copyright © 2020-2023  润新知