码迷,mamicode.com
首页 > 编程语言 > 详细

python全栈闯关--5-字典

时间:2019-09-07 11:05:24      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:tab   dde   二分查找   关系   ash   weight   already   数据类型   div   

1、数据类型的划分

数据类型分为:可变数据类型,不可变数据类型

    • 不可变数据类型:元祖、bool、int、str                    可哈希
    • 可变数据类型:list,dic,set                                   不可哈希

2、dic的数据结构

dic key:必须为可哈希数据类型,不可以变数据类型

      value:任意数据类型

dic 优点:二分查找,存储大量关系型数据

dic特点:3.6以前无序,3.6后有序

 

3、dic的增、删、改、查

定义

dic = {
    name: [bear, Honey, bee],
    myPthon: [{num1: 71, avg_age: 18}
                ,{num1: 71, avg_age: 18}
                ,{num1: 71, avg_age: 18}],
    True: 1,
    (1, 2, 3): bee,
    2: Honey
}
print(dic)

The key value is an immutable type(hash),which can be tuple,bool,int,str. (immutable:不可变)

 But when the key value is a tuple, it must be an immutable type.

A varable tuple like this is not posible:(1, 2, 3, [1, 2]).

It will reopot errors:TypeError: unhashable type: ‘list‘.

dic1 = {age: 18, name: bear, sex: male}
dic2 = {}
# if the key value does not exist to be added to the dictionary
dic1[high] = 185
# if the dictionary has a key value, update the corresponding value to the dictionary (corresponding:相符的)
dic1[age] = 15

# Key value does not exist, default add none to dictionary
dic1.setdefault(weight)
# The key value does note exist, if the second parameter is specified,add the (key,value) pair to the dictionary
dic1.setdefault(weight, 90)
# If the key value already exists,the specified value will not be update to the dictionary
dic1.setdefault(name, bee)

python全栈闯关--5-字典

标签:tab   dde   二分查找   关系   ash   weight   already   数据类型   div   

原文地址:https://www.cnblogs.com/zxw-xxcsl/p/11478580.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!