关于字典
- idea系列删除空格行的方法: <span style="background-color: rgb(255, 255, 255);">^[ \t]*\n 正则表达式空格 替换空行</span>
- # 字典是有 键 和 值 拼凑成的
- # 111 取字典 中的 所有的 键
- # 方法1
- # my_dict = dict(name="lowman", age=45, money=998, hourse=None)
- # print(my_dict)
- # key_list = my_dict.keys()
- # print(list(key_list))
- # 方法2
- # item = {"name": "lowman", "age": 27}
- # print(item)
- # data = set(item)
- # print(data)
- # 222取出里面的值
- # my_dict = dict(name="lowman", age=45, money=998, hourse=None)
- # value_list = my_dict.values()
- # print(list(value_list))
- #
- # # value = my_dict["age"] 没有就会异常 用第二种
- # value = my_dict.get("age") # 没有就会返回none
- # print(value)
- # 222遍历字典
- # my_dict = dict(name="lowman", age=45, money=998, hourse=None)
- # for item in my_dict:
- # print(item)
- #自带方法 items() 可以同时取出键与值
- # my_dict = dict(name="lowman", age=45, money=998, hourse=None)
- # # 取出值
- # for key, value in my_dict.items():
- # print(key, value)
- # # 取出元组
- # for item in my_dict.items():
- # print(item)
- # 删除键值对
- # my_dict = {"name": "lowman", "age": 12}
- # del my_dict["name"]
- # print(my_dict)
复制代码
|