day 05 字典

2018-10-24 08:43:37来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

一、字典的简单介绍

  • 字典(dict)是python中唯一的一个映射类型。是以{ }括起来的键值对组成。
  • 字典(dict)中key必须是不可变的,而value没有要求,可以保存任意类型的数据。

1)字典的保存原理:

    字典在保存的时候,采用的是hash算法:根据key来计算出一个内存地址,然后将key-value保存在这个地址中。所以,在dict中存储的key-value中的key必须是可hash的。

    对于可hash,暂可以理解为可以改变的都是不可hash的,那么可哈希就意味着不可变,这个是为了能准确的计算内存地址而规定的。

  • 已知的可hash(不可变)的数据类型:int, str, tuple, bool
  • 不可hash(可变)的数据类型:list, dict, set
  • dict保存的数据不是按照我们添加进去的顺序保存的,是按照hash表的顺序保存的,而hash表不是连续的。所以不能进行切片工作,只能通过key来获取dict中的数据。
dic = {"name": "Tom", "age": 18}

print(dic[name]) # Tom

二、字典的增删改查

1)增

  • dic[key] = value

     若字典中无此key,则添加键值对,若字典中有此key,则为修改value内容

dic = {"name": "Tom", "age":18}

dic["hobby"] = "football" 
print(dic)  # {"name": "Tom", "age": 18, "hobby": "football"}  增加键-值对
dic = {"name": "Tom", "age": 18}

dic["age"] = 20
print(dic) # {"name": "Tom", "age": 20} 修改value内容

 

  • setdefault

流程:

a) 判断key是否存在,如果存在,就不执行新增,返回key对应value内容

b) 如果不存在,执行新增,并返回key对应value内容

dic = {"name": "Tom", "age": 18}

ret = dic.setdefault("hobby", "football")
print(dic)  # {"name": "Tom", "age": 18, "hobby": "football"}
print(ret)  # football

 

dic = {"name": "Tom", "age": 18}

ret = dic.setdefault("name", "Andy")
print(dic)  # {"name": "Tom", "age": 18}
print(ret)  # Tom

 

当采用setdefault方法新增键值对时,若只提供key,则默认value为None

dic = {"name": "Tom", "age": 18}

dic.setdefault("hobby")
print(dic) # {"name": "Tom", "age": 18, "hobby": None}

 

2)删

  • pop

根据key删除,有返回值

dic = {"name": "Tom", "age": 18, "hobby": "football"}

ret = dic.pop("hobby")
print(dic)  # {"name": "Tom", "age": 18}
print(ret) # football

 

  • popitem

随机删除(3.5以下为随机删除,3.5以上为删除末尾的,但python底层为随机删除)

dic = {"name": "Tom", "age": 18, "hobby": "football"}

dic.popitem()
print(dic)  # {"name": "Tom", "age": 18}

 

  • del
根据key删除
dic = {"name": "Tom", "age": 18, "hobby": "football"}

del dic["hobby"]
print(dic)  # {"name": "Tom", "age": 18}

 

  • clear

清空字典

dic = {"name": "Tom", "age": 18, "hobby": "football"}

dic.clear()
print(dic)  # {}

 

3)改

  • dic[key] = value
dic = {"name": "Tom", "age": 18, "hobby": "football"}

dic["age"] = 20
print(dic) # {"name": "Tom", "age": 20, "hobby": "football"}
 

 

  • update

 dic.update(dic1):把dic1中的内容更新到dic中。如果key重名,则修改替换,如果不存在,则新增

dic = {"name": "Tom", "age": 18}
dic1 = {"name": "Andy", "hobby": "football"}

dic.update(dic1)

print(dic) # {"name": "Andy", "age": 18, "hobby": "football"}
print(dic1) # {"name": "Andy", "hobby": "football"}

 

4)查

  • print(dic[key])

当key不存在时,报错

dic = {"name": "Tom", "age": 18, "hobby": "football"}

print(dic["name"])  # Tom

 

  • get

当key不存在时,返回None

dic = {"name": "Tom", "age": 18, "hobby": "football"}


print(dic.get("name")) # Tom
print(dic.get("addr")) # None
  • setdefault

当key存在时,则返回value值,但当key不存在时,则新增key-value对。

dic = {"name": "Tom", "age": 18, "hobby": "football"}

print(dic.setdefault("name")) # Tom

 

 三、其他相关操作

1)dic.keys()

遍历字典所有key:

for k in dic.keys():
    print(k)
for k in dic:
    print(k)
    print(dic[k])

 

 

2)dic.values()

遍历字典所有value:

for v in dic.values():
    print(v)

 

3)dic.items()

遍历字典的最好方案:

for k, v in dic.items():
    print(k, v)

 四、字典的嵌套

字典里可以嵌套多层列表、字典

dic = {
        "name": "Tom",
        "age": 30,    
        "hobby": ["football", "baseball"],
        "wife": {
                "name": "Andy",
                "age": "28",
        }
}

print(dic.get("wife").get("name")) # Andy
print(dic["hobby"][1])  # baseball

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Python之字符串方法

下一篇:python多进程使用及线程池的使用方法