zoukankan      html  css  js  c++  java
  • python之路——字典

    字典 dictionary

    字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据。

    1. 可变和不可变

    1)可变数据类型:int, str, bool, tuple

    2)可变数据类型:list, dict, set

    3)字典的 key(键)value

    3) key 是唯一的且是不可变的的数据类型。

    4) value 是任意数据类型。3.5版本之前字典是无序的,3.6版本之后是有序的。

      

     字典的 增,删,改,查:

    1. 字典的增:

     1). 有 key 就有覆盖,没有 key 就添加

    #dic = {"name":"jin","age":18,"sex":"male"}
    #dic['name2'] = '依米'
    #print(dic)
    
    # dic['name'] = '王子'
    # print(dic)
    
    #dic['kye'] = value

    2). # setdeafult 在字典中添加键值对 (有 key 不变,没有 key 才添加)

    dic = {"name":"jin","age":18,"sex":"male"}
    # dic.setdefault('hobby')
    # print(dic) # {'name': 'jin', 'age': 18, 'sex': 'male', 'hobby': None}
    
    # dic.setdefault('hobby','开车')
    # print(dic)  # {'name': 'jin', 'age': 18, 'sex': 'male', 'hobby': '开车'}
    
    # dic.setdefault('name','taibai')
    # print(dic)
    View Code

    2.  字典的删:

    1). pop 按 key (键)删除,有返回值,并且如果没有此 key , 可设置返回值。

    dic = {"name":"jin","age":18,"sex":"male"}
    # ret = dic.pop('sex')
    # print(ret)
    
    # ret = dic.pop('sex1','没有此键')
    # print(ret)

    2). clear (清空)

    dic = {"name":"jin","age":18,"sex":"male"}
    
    # dic.clear()
    # print(dic)

    3). del 删除字典,删除键值对

    dic = {"name":"jin","age":18,"sex":"male"}
    
    # del dic['name']
    # print(dic)
    
    #del dic['age']
    #print(dic)

    4). popitem () 随机删除

    dic = {"name":"jin","age":18,"sex":"male"}
    
    # reb = dic.popitem()
    # print(reb)
    # print(dic)

    3. 字典的改:dic['key'] = value,有 key 就覆盖。

    # update (更新,更改,校正):将dic 的键值对,覆盖,添加dic2中,dic不变。

    dic = {"name":"jin","age":18,"sex":"male"}
    #dic2 = {'name':"alex",'weight':74}
    #dic2.update(dic)
    #print(dic)
    #print(dic2)

    4). 字典的查:

    dic = {"name":"jin","age":18,"sex":"male"}
    
    #print(dic['name'])
    # print(dic.get('name'))
    # print(dic.get('name2','没有此键值对'))

    3. 字典的键 (key),值(value),键值对 (item):

    dic = {"name":"jin","age":18,"sex":"male"}
    #keys = dic.keys()
    #print(keys,type(leys))
    
    # k = list(keys)
    # print(k,type(k))
    
    # values = dic.values()
    # print(values)
    
    # items = dic.items()
    # print(items)

    # for k,v in dic.items():
    # print(k,v)
     

    4. 字典的嵌套:

    dic = {
    'name':'金鑫',
    'name_list':[1,2,3,'李杰'],
    1:{
    'python10':['小黑','萌哥'],
    '老男孩':{'name':'oldboy','age':56}
    }
    }
    #1,[1,2,3,'李杰']追加'wusir'
    dic['name_list'].append('wusir')
    print(dic)
    #
    #2, ['小黑','萌哥'] 小黑 萌哥中间 插入一个花哥
    l = dic[1]['python10']
    l.insert(1,'花哥')
    print(dic)
    #
    #3, {'name':'oldboy','age':56} 添加一个键值对,hobby:男人
    dic1 = dic[1]
    dic2 = dic1['老男孩']
    dic2['hobby'] = '男人难'
    print(dic)
    
    dic['name'] = '王子'
    print(dic)
    
    dic.setdefault('hobby')
    print(dic)
    View Code
  • 相关阅读:
    sql中别名加as和不加as的区别
    easyui 扩展 datagrid 数据网格视图
    asp.net 版本一键升级,后台直接调用升级脚本
    SQLserver 还原数据库报“指定转换无效”的错的解决方案
    sql视图显示数据不对应
    django channels 实现实时通讯,主动推送
    django orm信号机制 + apschedule 定时任务
    django 使用原始SQL语句方式
    Django model 常用查询(搬运来备份的)
    Python3+ Django2.7开发web排坑记006 apache+wsgi部署应用
  • 原文地址:https://www.cnblogs.com/lzg-lzg/p/8351561.html
Copyright © 2011-2022 走看看