zoukankan      html  css  js  c++  java
  • python 学习_字典

    一. hash 函数

    可以被hash 那么 就是不可变的

    不可变类型

    可变类型

    数字

    列表

    字符串

    字典

    元组

     

     

     

     

     

     

     

     

    可变的数据类型不可以被hash,  一个值可以被hash,那么说明这个值是不可变的

     

     

    二.字典

     

    字典的特征:
      1. key-value 结构
      2. key 必须可hash 且必须为不可变数据类型, 必须唯一
      3. 可存放任意多个值, 可修改, 可不唯一
      4. 无序
      5. 查找速度快

    字典的创建与操作:

    字典的创建

    # 1)
    person = {"name":"augustyang","age":18}
    #2)
    person1 = dict(name="augustyang",age=18)
    #)3
    person2=dict({"name":"augustyang","age":18})
    #4)
    dit = {}.fromkeys(["k1","k2"],11)

    字典赋值

    dit["k1"]=222
    print(dit) #{'k1': 222, 'k2': 11}

     键值对

    print(dit.keys()) #dict_keys(['k1', 'k2'])
    
    print(dit.values()) #dict_values([222, 11])
    
    print(dit.items()) #dict_items([('k1', 222), ('k2', 11)])
    新增
    #1)
    dit["new_key"] = "new_value"
    
    #2)
    dit = {'k1': 222, 'k2': 11}
    dit.setdefault("key_1","key_value") # 如果字典中不存在key_1,由dic[key]=default
    print(dit)
    # {'k1': 222, 'k2': 11, 'key_1': 'key_value'}
    删除
    # 1)
    dit.pop(key[,default])
    
    # 如果字典中存在key,删除并返回key对应的vuale;
    # 如果key不存在,且没有给出default的值,则引发keyerror异常;

    dit.pop('key_1',"www") print(dit) #2) dit.clear() print(dit) #{}
    修改
    dit = {'k1': 222, 'k2': 11}
    dit['k1'] = 'new_value1'
    print(dit)  # {'k1': 'new_value1', 'k2': 11}
    
    #
    dit2 = {"name":'wwww',"k2":1122}
    dit.update(dit2)
    print(dit)
    查看
    print(dit['k1'])
    
    # dict.get(key, default = None)
    # 返回字典中key对应的值,若key不存在字典中,则返回default的值(default默认为None)
    print(dit.get('k1111','k1111_11')) # k1111_11

    循环

    for k in dic.keys()
    for k,v in dic.items()
     for k in dic  #*************
    长度
    print(len(dit))

     三 . 字典源码

    class dict(object):
        """
        dict() -> new empty dictionary
        dict(mapping) -> new dictionary initialized from a mapping object's
            (key, value) pairs
        dict(iterable) -> new dictionary initialized as if via:
            d = {}
            for k, v in iterable:
                d[k] = v
        dict(**kwargs) -> new dictionary initialized with the name=value pairs
            in the keyword argument list.  For example:  dict(one=1, two=2)
        """
        def clear(self): # real signature unknown; restored from __doc__
            """ D.clear() -> None.  Remove all items from D. """
            pass
    
        def copy(self): # real signature unknown; restored from __doc__
            """ D.copy() -> a shallow copy of D """
            pass
    
        @staticmethod # known case
        def fromkeys(*args, **kwargs): # real signature unknown
            """ Returns a new dict with keys from iterable and values equal to value. """
            pass
    
        def get(self, k, d=None): # real signature unknown; restored from __doc__
            """ D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None. """
            pass
    
        def items(self): # real signature unknown; restored from __doc__
            """ D.items() -> a set-like object providing a view on D's items """
            pass
    
        def keys(self): # real signature unknown; restored from __doc__
            """ D.keys() -> a set-like object providing a view on D's keys """
            pass
    
        def pop(self, k, d=None): # real signature unknown; restored from __doc__
            """
            D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
            If key is not found, d is returned if given, otherwise KeyError is raised
            """
            pass
    
        def popitem(self): # real signature unknown; restored from __doc__
            """
            D.popitem() -> (k, v), remove and return some (key, value) pair as a
            2-tuple; but raise KeyError if D is empty.
            """
            pass
    
        def setdefault(self, k, d=None): # real signature unknown; restored from __doc__
            """ D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """
            pass
    
        def update(self, E=None, **F): # known special case of dict.update
            """
            D.update([E, ]**F) -> None.  Update D from dict/iterable E and F.
            If E is present and has a .keys() method, then does:  for k in E: D[k] = E[k]
            If E is present and lacks a .keys() method, then does:  for k, v in E: D[k] = v
            In either case, this is followed by: for k in F:  D[k] = F[k]
            """
            pass
    dict 源码

     

     

     

     

  • 相关阅读:
    关于oralce字符集问题(复制别人的,纯属自己学习)
    Linux下oracle11gR2系统安装到数据库建立配置及最后oracle的dmp文件导入一站式操作记录
    Linux下部署ASP.NET服务连接oracle遇到的问题记录
    前端金钱分转元,元转分精度问题解决
    vue-element的form表单显示图片
    vue页面刷新技巧--(v-if指令)以及vue动态设置css属性
    vue后端获取的数据无法进行双向数据绑定
    Vue实现勾选框全选和局部选择功能
    VUE/jQuery生成二维码扫描跳转地址
    uni-app之数据状态改动后页面不刷新踩坑
  • 原文地址:https://www.cnblogs.com/augustyang/p/10437068.html
Copyright © 2011-2022 走看看