Python基础之五

2021/7/5 1:50:58

本文主要是介绍Python基础之五,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

# 创建字典
test_dict = {'key1': 'hello', 'key2': 'python'}
print(test_dict, type(test_dict))
test_dict_01 = dict(key1='123', key2='china')
print(test_dict_01, type(test_dict_01))
# 查询字典
print(test_dict['key1'])  # 方法一
print(test_dict.get('key1'))  # 方法二
print(test_dict.get('key3'))  # 没有key3则返回None
print(test_dict.get('key3', 'default'))  # 没有key3则返回默认值
# 获取字典中所有的key
res = test_dict.keys()
print(res, type(res))  # 获取所有的key,打印获取key的类型
res_1 = tuple(test_dict.keys())[0]  # 获取到的结果为字典所有的key,需要通过转换为tuple或者list才能获取相应的值
print(res_1, type(res_1))  # 单独获取第一个key并打印类型
# 获取字典中所有的value
res_2 = test_dict.values()
print(res_2, type(res_2))  # 获取所有的value,打印获取value的类型
res_3 = list(test_dict.values())
res_4 = list(test_dict.values())[0]
print(res_3, type(res_3))
print(res_4, type(res_4))
print('---' * 20)
# 获取所有key和value
res_5 = test_dict.items()
res_6 = list(test_dict.items())  # 转换成列表(列表里面包含元组)
res_7 = list(test_dict.items())[0]  # 取列表里面的第一个元素,即元组
print(res_5, type(res_5))
print(res_6, type(res_6))
print(res_7, type(res_7))
# 遍历
print('-----------遍历--------------')
for key in test_dict:  # 遍历所有的key
    print(key)
for value in test_dict.values():  # 遍历所有的value
    print(value)
for item in test_dict.items():  # 遍历所有的键值对
    print(item)
for key, value in test_dict.items():  # 遍历所有的key、value
    print(key, value)
# 修改
print('---------------修改------------------')
test_dict_02 = {'key1': 'welcome', 'key2': 'to', 'key3': 'Guangzhou'}
test_dict_03 = {}
test_dict_02['key1'] = '欢迎'  # 原字典存在即替换
test_dict_02['key4'] = '中国'  # 原字典不存在则添加
test_dict_03['key1'] = '欢迎'  # 空字典直接添加
print(test_dict_02)
print(test_dict_03)
test_dict_04 = {'key1': 'welcome', 'key2': 'to', 'key3': 'Guangzhou'}
test_dict_04.setdefault('key1', '888')  # key1存在则不改变
print(test_dict_04)
test_dict_04.setdefault('key5', '999')  # key5不存在则添加键值对
print(test_dict_04)
test_dict_05 = {'key1': 'val1', 'key2': 'val2'}
test_dict_06 = {'key1': '替换', 'key3': '添加'}
test_dict_05.update(test_dict_06)  # 将test_dict_06合并到test_dict_05,如有重复则替换原值,不重复则添加
print(test_dict_05)
# 删除
print('--------------------删除----------------------')
test_dict_06 = {'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}
test_dict_07 = {'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}
test_dict_08 = {'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}
del test_dict_06['key2']  # 删除键值对,没有返回值
print(test_dict_06)
res = test_dict_07.pop('key2')  # 删除键值对,并返回删除键值对的值
print(res, test_dict_07)
res = test_dict_08.popitem()  # Python进出栈的规则,后进先删,字典左边为先进,右边为后进
print(res, test_dict_08)
test_dict_08.clear()  # 清空
print(test_dict_08)
# -*- coding: utf-8 -*-
# @Time: 2021/7/2 20:54
# @Author: 阿炳
# @File: D6_Demo01.py

import copy  # 导入copy包
# 浅拷贝
# 理解:1、不管是修改原字典还是浅拷贝后的字典中的“可变”元素,原字典和新拷贝的字典两者内容都会发生改变
# 2、浅拷贝其实也有部分深拷贝在里面,就是“不可变”元素,修改其中一方的不可变元素的值,
# 另一方是不会发生改变的,这部分其实就是深拷贝
test_dict_1 = {'key1': 'hello', 'key2': [1, 3, 'China']}  # 原字典
new_copy_dict1 = test_dict_1.copy()  # 浅拷贝字典
print(test_dict_1, id(test_dict_1))  # 打印原字典和内存地址
print(new_copy_dict1, id(new_copy_dict1))  # 打印浅拷贝字典和内存地址
print('--------------------------------------')
test_dict_1['key2'][0] = 'HELLO'  # 修改“原字典”的“可变”元素(list)的值
test_dict_1['key1'] = '你好'  # 修改“原字典”中“不可变”元素
print(test_dict_1, id(test_dict_1))  # 打印修改后的原字典和内存地址
print(new_copy_dict1, id(new_copy_dict1))  # 打印浅拷贝字典和内存地址
# 以上可以发现浅拷贝修改其中一个字典的可变元素后,浅拷贝字典的内容也会跟着发生变化
# 但是浅拷贝中的深拷贝并不会跟随一方而变化
print('******************************************')
# 深拷贝
# 理解:不管是修改原字典还是浅拷贝后的字典中的“可变”元素还是“不可变”元素,原字典和新拷贝的字典两者内容都不会发生改变
test_dict_2 = {'key1': 'hello', 'key2': [2, 4, 'Python']}  # 原字典
new_copy_dict2 = copy.deepcopy(test_dict_2)  # 深拷贝字典
print(test_dict_2, id(test_dict_2))  # 打印原字典和内存地址
print(new_copy_dict2, id(new_copy_dict2))  # 打印深拷贝字典和内存地址
print('--------------------------------------')
test_dict_2['key2'][0] = 888  # 修改“原字典”的“可变”元素(list)的值
test_dict_2['key1'] = '你好'  # 修改“原字典”中“不可变”元素
print(test_dict_2, id(test_dict_2))  # 打印修改后的原字典和内存地址
print(new_copy_dict2, id(new_copy_dict2))  # 打印深拷贝字典和内存地址
# 以上可以发现深拷贝修改其中一个字典的可变元素或不可变元素,深拷贝另一个字典的内容都不会跟随发生变化

# 总结
# 共同点:
# 1、拷贝后内存地址都不一样
# 2、“不可变”元素都是深拷贝

# 不同点:
# 1、浅拷贝其中一方的“可变”元素修改后,另一方也同步(因为指向的内容其实是同一个);深拷贝的“可变”元素修改后,另一方不会跟随发生变化




"""
1、基础语法
2、运算符
3、字符串
4、列表
5、元组
6、字典
7、集合
8、流程控制
9、函数
10、类


一、字典(增删改查)
特性【掌握】
1、key,value键值对表达字典
2、key是唯一不可重复,values可以重复
3、字典无序

1、创建字典【掌握】
   1.1、test_dict = {"key1":"val1","key2":"val2"}
   1.2、test_dict = dict(key1="val1",key2="val2")
   1.3、test_dict = {}
        test_dict["key3"]=8888
        test_dict["key1"]=8888
        test_dict["key4"]=8888
2、查询【掌握】
   2.1、test_dict["key2"]  如果key 不存在会报错KeyError: 'key3'
   2.2、res = test_dict.get("key3","val3") 如果key不存在,会返回默认值val3,不是添加到字典中
   2.3、test_dict = {"key1":"val1","key2":"val2","key3":"val3"}
        res = list(test_dict.keys())
        print(res,type(res))
        获取到的结果为字典的所有key,需要通过list进行转换才能获取对应的值
   2.4、test_dict = {"key1":"val1","key2":"val2","key3":"val3"}
        res=list(test_dict.items())
        print(res)
        获取所有的key,value,返回key与val对应的元组
   2.5、test_dict = {"key1":"val1","key2":"val2","key3":"val3"}
        res = list(test_dict.values())
        print(res,type(res))
        获取到的结果为字典的所有value,需要通过list进行转换才能获取对应的值  
   2.6、支持遍历【目前了解】
        [1,2,3,4,5,6]
        遍历key
        遍历values
        遍历key,values
3、修改
   3.1、test_dict["key3"]=8888 【常用】
        通过key修改value,存在就会替换掉原来的value,如果key不存在,会新增一个键值对
   3.2、test_dict.setdefault("key4","888")【用得少】
        通过key添加键值对,key存在不做任何操作,key不存在相当于添加键值对
        注意与test_dict["key3"]=8888方法区别
   3.3、test_dict_1.update(test_dict_2) 【常用】
        将test_dict_2合并到test_dict_1
        key重复:就会被覆盖
        key不重复:将test_dict_2合并到test_dict_1
        
4、删除【不用】
   4.1、del dict["key"]
        通过key删除对应的键值对,无返回值
   4.2、test_dict_1.pop("key1")
         通过key删除对应的键值对,返回被删除key对应的value
   4.3、res = test_dict_1.popitem()
        随机删除一个键值对,返回删除的键值对,按python进栈出栈规则:后进先出原则进行删除
        
5、排序【了解】
排序使用场景:鉴权,字典排序后进行加密
1、请求参数:
{"key1":"val1","key2":"val2","key3":"val3"}
2、rsa 加密   
hhhdkjasshfkjahsd
3、组合请求参数
{"key1":"密串","key2":"val2","key3":"val3","sign":"hhhdkjasshfkjahsd"}
4、operator.itemgetter(1)
   0:根据字典的key的ascii码进行排序
   1:根据字典的value的ascii码进行排序
6、深浅拷贝
"""


这篇关于Python基础之五的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程