• Python字典部分源码分析,字典是无序的


    1 def clear(self): # real signature unknown; restored from __doc__
    2         """ D.clear() -> None.  Remove all items from D. """
    3         pass
    1 #练习1、清空字典(置空)
    2 li = {"key1":"value1","key2":"value2"}
    3 li.clear()     #清空所有
    4 print(li)
    5 #执行结果:
    6 {}
    1 def copy(self): # real signature unknown; restored from __doc__
    2         """ D.copy() -> a shallow copy of D """
    3         pass
    1 #练习2、浅拷贝
    2 li = {"key1":"value1","key2":"value2","key3":"value3"}
    3 dd = li.copy()    #浅拷贝
    4 print(li)
    5 print(dd)
    6 #执行结果:
    7 {'key2': 'value2', 'key3': 'value3', 'key1': 'value1'}
    8 {'key2': 'value2', 'key3': 'value3', 'key1': 'value1'}
    1  @staticmethod # known case
    2     def fromkeys(*args, **kwargs): # real signature unknown
    3         """ Returns a new dict with keys from iterable and values equal to value. """
    4         pass
    1 #练习3、迭代字符串生成同一值,放到新字典
    2 li = {"key1":"value1","key2":"value2","key3":"value3"}
    3 xin = li.fromkeys("key1","444")
    4 print(xin)
    5 aa = li.fromkeys("ttb","value1")  #分别迭代字符串生成同一个值,放在新字典中,与原字典好像没什么关系
    6 print(aa)
    7 #执行结果:
    8 {'1': '444', 'y': '444', 'e': '444', 'k': '444'}
    9 {'b': 'value1', 't': 'value1'}
    1 def get(self, k, d=None): # real signature unknown; restored from __doc__
    2         """ D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None. """
    3         pass
    1 #练习4、获取指定键的值
    2 li = {"key1":"value1","key2":"value2","key3":"value3"}
    3 aa = li.get("key2")    #获取指定键的值
    4 print(aa)
    5 #执行结果
    6 value2
    1 def items(self): # real signature unknown; restored from __doc__
    2         """ D.items() -> a set-like object providing a view on D's items """
    3         pass
    1 #练习5、获取字典所有内容
    2 li = {"key1":"value1","key2":"value2","key3":"value3"}
    3 aa = li.items()    #获取字典所有
    4 print(aa)
    5 #执行结果:
    6 dict_items([('key1', 'value1'), ('key3', 'value3'), ('key2', 'value2')])
    1 def keys(self): # real signature unknown; restored from __doc__
    2         """ D.keys() -> a set-like object providing a view on D's keys """
    3         pass
    1 练习6、获取字典中所有键
    2 li = {"key1":"value1","key2":"value2","key3":"value3"}
    3 aa = li.keys()    #获取字典所有键
    4 print(aa)
    5 #执行结果:
    6 dict_keys(['key2', 'key3', 'key1'])
    1 def pop(self, k, d=None): # real signature unknown; restored from __doc__
    2         """
    3         D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
    4         If key is not found, d is returned if given, otherwise KeyError is raised
    5         """
    6         pass
    1 #练习7、删除指定键
    2 li = {"key1":"value1","key2":"value2","key3":"value3"}
    3 aa = li.pop("key2")    #删除指定键,连带删除值
    4 print(li)
    5 #执行结果
    6 {'key1': 'value1', 'key3': 'value3'}
    1 def popitem(self): # real signature unknown; restored from __doc__
    2         """
    3         D.popitem() -> (k, v), remove and return some (key, value) pair as a
    4         2-tuple; but raise KeyError if D is empty.
    5         """
    6         pass
    1 #练习8、随机删除键值
    2 li = {"key1":"value1","key2":"value2","key3":"value3"}
    3 aa = li.popitem()    #随机删除指定键,连带删除值
    4 print(li)
    5 #执行结果:()随机删掉一个
    6 {'key3': 'value3', 'key1': 'value1'}
    1 def setdefault(self, k, d=None): # real signature unknown; restored from __doc__
    2         """ D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """
    3         pass
     1 #练习9、输出指定键值,键不存在则加入字典
     2 li = {"key1": "value1", "key2": "value2", "key3": "value3"}
     3 aa = li.setdefault("key1","111")  # 键存在字典中时,相当于li.get("key1"),不管后面加什么都没影响
     4 print(aa)
     5 bb = li.setdefault("kkk","111")   #键不存在时,输入值,并且该键值会加入到字典中
     6 print(bb)
     7 print(li)
     8 执行结果:
     9 value1
    10 111
    11 {'key2': 'value2', 'key1': 'value1', 'kkk': '111', 'key3': 'value3'}
    1 def update(self, E=None, **F): # known special case of dict.update
    2         """
    3         D.update([E, ]**F) -> None.  Update D from dict/iterable E and F.
    4         If E is present and has a .keys() method, then does:  for k in E: D[k] = E[k]
    5         If E is present and lacks a .keys() method, then does:  for k, v in E: D[k] = v
    6         In either case, this is followed by: for k in F:  D[k] = F[k]
    7         """
    8         pass
     1 #练习10、更新字典
     2 li = {"key1": "value1", "key2": "value2", "key3": "value3"}
     3 aa = {"key1":"111","key2":"222"}
     4 li.update(aa)      #aa是新的,把新的放进旧的,如果键相同则改变值,如果键不同则直接加入
     5 print(li)
     6 li1 = {"key1": "value1", "key2": "value2", "key3": "value3"}
     7 aa1 = {"key1":"111","key2":"222"}
     8 aa1.update(li1)
     9 print(aa1)
    10 #执行结果:
    11 {'key3': 'value3', 'key1': '111', 'key2': '222'}
    12 {'key3': 'value3', 'key1': 'value1', 'key2': 'value2'}
    1 def values(self): # real signature unknown; restored from __doc__
    2         """ D.values() -> an object providing a view on D's values """
    3         pass
    1 #练习11、输出字典所有值
    2 li = {"key1": "value1", "key2": "value2", "key3": "value3"}
    3 aa = li.values()    #输出字典所有的值
    4 print(aa)
    5 执行结果:
    6 dict_values(['value3', 'value2', 'value1'])
  • 相关阅读:
    使用flv.js + websokect播放rtsp格式视频流
    form表单数据回显双向绑定失效的原因
    element UI日期选择器动态切换了type之后布局错乱
    umi+dva+antd+axios搭建项目,跨域代理问题
    浏览器-preview 改写了 response里面的int类型的字段数值
    mac笔记本分辨率为2560*1600,css样式错位问题
    常用的正则表达式
    vue 实现树形结构
    js禁止遮罩层下页面滚动
    ts封装axios
  • 原文地址:https://www.cnblogs.com/repo/p/5415045.html
Copyright © 2020-2023  润新知