dict:
- 创建:
d = {"dog":101, "cat":89, "lion":57, "tiger":309}
- 取值:
print(d["lion"]) ----------------> 57
- 设值:
d["cat"] = 90
d["bird"] = 180
print(d) ----> {'tiger': 309, 'bird': 180, 'lion': 57, 'dog': 101, 'cat': 90}
- 判断key是否存在,用in或get()方法:
print("fish" in d) ----------------> False
print(d.get("fish")) ----------------> None
- 删除:
d.pop("tiger")
print(d) ----> {'bird': 180, 'lion': 57, 'dog': 101, 'cat': 90}
网友评论