zoukankan      html  css  js  c++  java
  • python基础_字典

    empty_dict = {}
    a_dict = {'one':1,'two':2,'three':3}
    print("{}".format(a_dict))
    print("{}".format(len(a_dict)))
    another_dict = {'x':'printer','y':5,'z':['star','circle',9]}
    print("{!s}".format(len(another_dict)))
    print("{}".format(another_dict['z']))
    a_new_dict = a_dict.copy()
    print("{}".format(a_new_dict))
    print("{}".format(a_dict.keys()))
    print("{}".format(a_dict.values()))
    print("{}".format(a_dict.items()))
    if 'c' not in another_dict:
    print("{}".format(another_dict.keys()))
    print("{}".format(a_dict.get('three')))
    print("{}".format(a_dict.get('four')))
    print("{}".format(a_dict.get('four','not in dict')))

    {'one': 1, 'two': 2, 'three': 3}
    3
    3
    ['star', 'circle', 9]
    {'one': 1, 'two': 2, 'three': 3}
    dict_keys(['one', 'two', 'three'])
    dict_values([1, 2, 3])
    dict_items([('one', 1), ('two', 2), ('three', 3)])
    dict_keys(['x', 'y', 'z'])
    3
    None
    not in dict

    a_dict = {'one':1,'two':2,'three':3}
    dict_copy = a_dict.copy()
    ordered_dict1 = sorted(dict_copy.items(),key=lambda item:item[0])
    print("{}".format(ordered_dict1))
    ordered_dict2 = sorted(dict_copy.items(),key=lambda item:item[1])
    print("{}".format(ordered_dict2))
    ordered_dict3 = sorted(dict_copy.items(),key=lambda x:x[1],reverse=True)
    print("{}".format(ordered_dict3))

    [('one', 1), ('three', 3), ('two', 2)]
    [('one', 1), ('two', 2), ('three', 3)]
    [('three', 3), ('two', 2), ('one', 1)]
  • 相关阅读:
    $Noip2018/Luogu5022$ 旅行
    $Noip2018/Luogu5020$ 货币系统 $dp$
    $Noip2018/Luogu5021$ 赛道修建 二分+树形
    $Noip2018/Luogu5019/Luogu1969$ 铺设道路
    $Poj1220/AcWing124 Number Base Convertion$ 进制转换+高精除
    $Poj1050 To the Max$
    $Poj1723/AcWing123 Soldiers$ 排序
    luogu质数距离
    模板线性筛
    CERC2016 bfs 最大生瓶颈边 lca
  • 原文地址:https://www.cnblogs.com/wei23/p/13157709.html
Copyright © 2011-2022 走看看