我爱编程

Python dict 的方法

2018-07-26  本文已影响0人  智勇双全的小六

参考教程:
https://blog.csdn.net/jeapeducom/article/details/26448481

一 dict 初始化有五种方法

  1. d = {}
  2. d = dict()
  3. d = dict(mapping)
    这个 mapping object’s (key, value) pairs 比较难理解,其实就是一个用 map 生成的 (key, value) 键值对。
dict(map(lambda x,y:(x,y),[1,2,3,4,5,6],[0,1,2,3,4,5]))
  1. d = dict(iterable)
  2. dict(**kwargs)
dict(one=1,two=2,...)

二 dict 对象所具有的方法

w = {"a":1}
h = w.setdefault("b",[])
print(h) // []
print(w) // {"a":1,"b":[]}
h.append(2)
print(w) // {"a":1,"b":[2]}

所以,我看到了这种写法:

w.append("c",[]).append("666")
w.pop("hello",None)
w.clear()
if w.get("x") is not None:
  w.pop("x")
上一篇 下一篇

猜你喜欢

热点阅读