python全栈闯关--5-字典

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‘)

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

时间: 2024-07-30 16:22:30

python全栈闯关--5-字典的相关文章

python全栈闯关--7-基础数据类型汇总、集合、深浅拷贝

1.str s = ' ' print(s.isspace()) # 如果字符串全是空格返回True 2.在循环一个列表时,最好不要删除列表中的元素 删除列表元素,得到的结果,往往不是预期的 例子1: lis = [11, 22, 33, 44, 55] # 循环开始后,按照索引递增,删除了前面的值,不会处理列表索引的位置 # 虽然没有报错,但是不是想要的结果 for i in lis: print("目前删除元素值为:%d " % i) print("值删除前:"

python全栈闯关--XX-细节总结汇总

1.The start value of slice is less than then end value,and the step is negative print("if the start value less then the end value and the step is negative ! " "No error will be reported and empty will be returned, ") for i in range(0,

python全栈闯关--6-小知识点总结

1.python2和python3区别常用整理 # python2print 'abc'# 返回一个列表range()# 返回一个生成器值,不是list.每次只生成一个值,避免暂用很大的内存xrange() # 生成器# 输入raw_input(...) # python3print('abc')# 返回一个迭代值,需要生成列表就需要使用list(range(...))range()input(...) 2.复制 == 比较值是否相等 is 比较,比较的是内存地址 id(内容) >>>

python全栈闯关--10-2、函数的嵌套和作用域

1.全局作用域函数嵌套 def three_max(a, b, c): t = two_max(a, b) # 函数嵌套调用two_max return t if t > c else c def two_max(a, b): return a if a > b else b print(three_max(1, 2, 3)) 程序执行,按照从下往下的顺序,把变量.函数名加入到命名空间,如果还未加入到命名空间,就调用了函数,将会报错. 如上程序,如果把three_max的调用,交换到two_m

python全栈闯关--11-装饰器初识

1.装饰器形成 当不想修改原函数,未函数前后添加功能时,就可以使用装饰器,在函数前后增加功能. 装饰器的初始形成 import time def timer(f): def inner(): print("我是装饰器,我来装饰了!!!") start = time.time() f() end = time.time() print(start - end) return inner # 返回inner由于f实现了闭包,直接调用了程序 def func(): time.sleep(1)

python全栈闯关--12-装饰器进阶

带参数的装饰器 装饰器使用过程中,如果需要一个参数来判断装饰器是否启用时,就需要传入一个参数,来判断是否启用. 目前装饰器,传输函数名给外部函数做参数,内部函数参数用做调用函数的参数,无法实现参数的传递. 因此,需要在加一层嵌套,来实现参数的传入,装饰器最多三层!!! import time from functools import wraps FLAG = True def out_warpper(flag): def warpper(f): @wraps(f) def w_in(): if

python全栈闯关--15-内置函数

1.作用域相关 print(locals()) print(globals()) # global 关键字,局部变量可以修改全局变量 # nonlocal 关键字,局部函数中,可以修改上一层函数中的变量 2.迭代器相关 next(迭代器) 等价与迭代器.__next__() 3.内置属相查看 dir 查看函数拥有的方法 4.帮忙 help 返回函数的帮助信息 5.程序调度相关 callable 判断函数是否可以执行,如果是函数返回true,是变量,放回false 6.文件操作相关 open 打开

Python全栈之路----数据类型—字典

字典:可变,一种key-value的数据类型 info = { 'stu1101' : 'TengLan Wu' , 'stu1102' : 'LongZe Luola' , 'stu1103' : ' XiaoZe Maliya' } 1.特点:key-value结构:key必须可hash(被hash值不变),且必须唯一.必须为不可变类型:无序的(因为无索引,通过key查询):查找速度快      2.基本操作:创建,添加,查找,修改,删除,清空 1 >>> info = { 2 ..

Python全栈开发【基础三】

Python全栈开发[基础三]  本节内容: 函数(全局与局部变量) 递归 函数 一.定义和使用 函数最重要的是减少代码的重用性和增强代码可读性 1 def 函数名(参数): 2 3 ... 4 函数体 5 ... 6 返回值 函数的定义主要有如下要点: def:表示函数的关键字 函数名:函数的名称,日后根据函数名调用函数 函数体:函数中进行一系列的逻辑计算 参数:为函数体提供数据 返回值:当函数执行完毕后,可以给调用者返回数据. 总结使用函数的好处: 1.减少代码重用 2.保持一致性,易维护