总结
总结容器:统一管理数据
字符串str:储存字符编码值,不可变,序列
列表list:储存变量,可变,序列元组
tuple:储存变量,不可变,序列
字典dict:储存键值对,可变,散列
键不能重复且不可变
集合set:储存键,可变,散列
可变和不可变的区别
不可变:数据在内存中本质都是不可变,采用按需分配的存储机制
可变:具有扩容能力,采用预留空间的存储机制
序列和散列的区别
序列:相邻有序,定位灵活(索引、切片)
散列:分散无序,定位迅速(键)
最常见的容器(选择使用哪种容器)
列表:存储单一维度的数据,例如:治愈人数列表,地区列表
字典:存储多个维度的数据,例如:学生信息,疫情列表
1、字符串
定义
由一系列字符组成的不可变序列容器,存储的是字符的编码值
编码
字节byte:计算机最小存储单位,等于8 位bit.
字符:单个的数字,文字与符号。
字符集(码表):存储字符与二进制序列的对应关系
编码:将字符转换为对应的二进制序列的过程。
解码:将二进制序列转换为对应的字符的过程。
编码方式:
--ASCII编码:包含英文、数字等字符,每个字符1个字节。
--GBK编码:兼容ASCII编码,包含21003个中文;英文1个字节,汉字2个字节。
--Unicode字符集:国际统一编码,旧字符集每个字符2字节,新字符集4字节。
--Unicode字符集:国际统一编码,旧字符集每个字符2字节,新字符集4字节。
--Unicode字符集:国际统一编码,旧字符集每个字符2字节,新字符集4字节。
--UTF-8编码:Unicode的存储与传输方式,英文1字节,中文3字节。
相关函数
# 2. 编码
# https://unicode-table.com/cn/#4E00
# 字 --> 数
print(ord("一")) # 19968
print(chr(19968)) # "一"
字面值
单引和双引号的区别
# 1. 写法
name01 = "悟空" # (推荐)
name02 = '悟空'
# 2.
# 单引号内的双引号不算结束符
# 双引号内的单引号不算结束符
message = '我是"孙悟空"。'
message = "我是'孙悟空'。"
三引号作用
# 可见即所得
name03 = """
悟
空"""
name04 = '''悟空'''
转义字符
# 3. 转义字符:改变原始含义的特殊字符
# \" \' 换行\n \\
message = "我是\"孙悟空\"。"
print(message)# 我是"孙悟空"。
message = "我是\n孙悟空。"
print(message)#
url = "\\a\\b\c\d\e.txt"
# 原始字符:后面的字符串木有转义字符
url = r"\a\b\c\d\e.txt"
print(url)
字符串格式化
定义:
生成一定格式的字符串
语法:
字符串%(变量)
# 灵活的数据
name = "唐僧"
age = 2
score = 100.51
# 按照固定的格式显示
# 字符串拼接(格式复杂代码可读性差)
# print("我是" + name + "今年" + str(age)
# + "考试" + str(score) + "分")
# 占位符%s
print("我是%s今年%.2d考试%.1f分" % (name, age, score))
usd = "15"
rmb = float(usd) * 7.1465
# print(usd + "美元 =" + str(rmb) + "人民币")
print("%s美元 = %.2f人民币"%(usd,rmb))
#判断
#isspace() 判断是否全都为空白字符
# res = 'this is a test string'.isspace()
# print(res)
# res = ' '.isspace()
# print(res)#True
#isalpha() 判断是否只包含字母
# res = 'abcd'.isalpha()
# print(res)
#isdigit() 判断是否只包含数字字符
# res = '12345'.isdigit()
# print(res)
#isdigit() 判断是否只包含数字字母
# res = '1234abcd'.isalnum()
# print(res)
# str1 = 'this is a test string'
# res = str1.startswith('this')
# print(res)
# res = str1.endswith('ing')
# print(res)
# 查找
str1 = 'this is a test string'
# 012
res = str1.find('is')
print(res)
res = str1.find('that')
print(res)
res = str1.count('at')
print(res)
# str1 = 'this is a test string'
# res = str1.replace('is','at',1)
# print(str1)
# print(res)
# str1 = ' hello '
# res = str1.strip()
# str1 = '**hello**'
# res = str1.strip('*')
# res = str1.lstrip('*')
# res = str1.rstrip('*')
# print(res)
2、列表(list)
定义
由一系列变量组成的可变序列容器
基础操作
创建列表
# 列表名称 = [元素1,元素2,元素3]
list01 = ["香港", "内蒙古", "四川"]
list02 = [51, 25, 16]
list3 = ["a", "b", "c", "d"]
# 列表名称 = list(其他容器)
list01 = list("孙悟空","猪八戒")
添加元素
列表名.append(元素),是在列表的最后依次添加元
list_city = ["香港", "内蒙古", "四川"]
list_city.append("山西")
print(list_city)
列表.insert(索引,元素),找到列表中元素的索引位置添加元素
list_now_people = [51, 25, 16]
list_now_people.insert(1, 0)
print(list_now_people)
定位元素
切片:
容器名[开始:结束:间隔] 不包含结束索引的元素
容器名[开始:结束] 间隔默认为1
容器名[:结束] 开始默认为0
容器名[:] 开始默认为0
容器名[::-1] 从尾到头
message = "我是花果山水帘洞美猴王齐天大圣"
print(message[0:3:1]) # 我是花
print(message[0:8:2]) # 我花山帘
print(message[0:3]) # 我是花
print(message[:3]) # 我是花
print(message[:]) # 我是花果山水帘洞美猴王齐天大圣
print(message[3:]) # 果山水帘洞美猴王齐天大圣
print(message[3:-1]) # 果山水帘洞美猴王齐天大
print(message[::-1]) #圣大天齐王猴美洞帘水山果花是我
print(message[2:250]) # 花果山水帘洞美猴王齐天大圣
print(message[2:7:-1]) #空
print(message[7:2]) #空
- 读取和修改元素
list_names = ["谭锦岳", "杨淮靖", "张昆鹏"]
# 1. 索引
item = list_names[0] # 读取第一个元素
list_names[0] = "tjy" # 修改第一个元素
# 2. 切片
# 通过切片读取元素时会创建新列表
items = list_names[1:] # 读取后两个元素
# 通过切片修改元素时会遍历右侧数据,依次存入左侧定位的区域
list_names[1:] = ["yhj", "zkp"] # 修改后两个元素
# list_names[1:] = "行吗" # ['tjy', '行', '吗']
# 左侧切片1个位置 右侧4个数据
list_names[1:1] = ["a", "b", "c", "d"]
# 左侧切片全部位置 右侧0个数据
# list_names[:] = []# 列表没有元素
print(list_names) # ['tjy', 'a', 'b', 'c', 'd', 'yhj', 'zkp']
遍历列表
- 正向
for 变量名 in 列表名:
变量名就是元素
# for number in range( 开始 ,结束 ,间隔 ):
# for number in range( 开始 ,结束):
# for number in range(结束):
list_planet = ["水星", "金星", "火星", "木星", "地球"]
list_planet[len(list_planet):] = ["土星", "天王星", "海王星"]
for i in range(0,len(list_planet),1):
print(list_planet[i])
for i in range(0,len(list_planet)):
print(list_planet[i])
for i in range(len(list_planet)):
print(list_planet[i])
- 反向
for 索引名 in range(len(列表名)-1,-1,-1):
列表名[索引名]就是元素
list_planet = ["水星", "金星", "火星", "木星"]
for i in range(len(list_planet) - 1, -1, -1):
print(list_planet[i])
删除元素
列表名.remove(元素)
list_names = ["谭锦岳", "杨淮靖", "张昆鹏"]
# 1. 根据元素删除
# 列表名.remove(元素)
list_names.remove("杨淮靖")
del 列表名[索引或切片]
list_names = ["谭锦岳", "杨淮靖", "张昆鹏"]
# 2. 根据定位删除
del list_names[0]
del list_names[:]
print(list_names)
删除不存在的元素,程序会报错,所以先判断
# 删除不存在的元素,会报错。
# 建议先判断,如果存在再删除.
if "老张" in list_names:
list_names.remove("老张")
深拷贝和浅拷贝
浅拷贝:复制过程中,只复制一层变量,不会复制深层变量绑定的对象的复制过程。
深拷贝:复制整个依懒的变量。
列表与字符串转换
列表 --> 字符串
将多个字符串拼接为一个列表
# 需求:根据xxx逻辑拼接字符串
# 缺点:每次拼接+ 都会产生新字符串对象(之前的数据就成为了垃圾)
# 解决方案核心思想:使用可变对象代替不可变对象
在终端中循环录入疫情城市,如果录入空字符串则停止.
list_city = []
while True:
city = input("请输入疫情地区:")
if city == "":
break
list_city.append(city)
str_city = "-".join(list_city)
print(str_city)
字符串 -->列表
将一个字符串拆分为多个
# 用一个字符串存储多个信息
# 可以使用split分割多个信息
"""
将下列英文语句按照单词进行翻转.
To have a government that is of people by people for people
people for people by people of is that government a have To
"""
message = 'To have a government that is of people by people for people'
list_temp = message.split(' ')
result = ' '.join(list_temp[::-1])
print(result)
列表推导式
使用简易方法,将可迭代对象转换为列表
根据可迭代对象构建列表时,使用列表推导式
# 语法
变量 = [表达式 for 变量 in 可迭代对象]
变量 = [表达式 for 变量 in 可迭代对象 if 条件]
list01 = [34, 45, 5, 65, 76, 8]
#判断上面列表中的元素>10的元素,然后组成一个新的列表
# 快捷键:iter + 回车
# list_result = []
# for number in list01:
# if number > 10:
# list_result.append(number)
# print(list_result)
#使用列表推导式
list_result = [number for number in list01 if number > 10]
print(list_result)
列表的扩容机制
list01 = [1,2,3,4,5]
#extend() 扩展 使用序列扩展列表 将序列中的内容追加到列表末尾
list01.extend('abc')
print(list01)
#向索引值为0的位置插入数据0
# list01.insert(0,0)
# print(list01)
# list01.append(6)
# print(list01)
# res = list01.index('a')
# print(res)
# res = list01.pop(0)
# print(res)
# print(list01)
3、元组(tuple)
定义
由一系列变量组成的不可变序列容器
基础操作
创建元组
# -- 通过元素创建
tuple01 = (10, 20, 30)
# -- 通过其他容器创建
list01 = [40, 50]
tuple02 = tuple(list01)
获取元素
tuple01 = (10, 20, 30)
# 2. 定位
# -- 索引
print(tuple01[-1])
遍历元组
tuple01 = (10, 20, 30)
#正向遍历
for item in tuple01:
print(item)
#反向遍历
for i in range(len(tuple01) - 1, -1, -1):
print(tuple01[i])
特殊能力
# 4. 特殊
# -- 如果元组只有1个元素,必须写逗号
tuple03 = (50,)
# -- 可以省略小括号
# tuple04 = (50,60,70)
tuple04 = 50, 60, 70
# -- 拆包(只要是容器都可以)
# a, b = (80, 90)
# a, b = [80, 90]
# a, b = "悟空"
a, *b = (80, 90, 100)
print(a) # 80
print(b) # [90, 100]
作用
- 元组与列表都可以存储一系列变量,由于列表会预留内存空间,所以可以增加元素。
- 元组会按需分配内存,所以如果变量数量固定,建议使用元组,因为占用空间更小
- 应用
变量交换的本质就是创建元组:x, y = (y, x )
格式化字符串的本质就是创建元祖:"姓名:%s, 年龄:%d" % ("tarena", 15)
4、字典(dict)
定义
由一系列键值对组成的可变散列容器。
散列:对键进行哈希运算,确定在内存中的存储位置,每条数据存储无先后顺序
键必须惟一且不可变(字符串/数字/元组),值没有限制。
基础操作
创建
使用元素创建字典
# --使用元素创建
dict01 = {"jxl": 100000, "lxn": 100000, "bj": 200000}
使用其他元素创建
# --使用其他容器(该容器内的元素必须能够一分二)
list01 = [("王豪", 50000), ["文剑", 60000], "美娟"]
# {'王豪': 50000, '文剑': 60000, '美': '娟'}
dict02 = dict(list01)
print(dict02)
添加
dict01 = {"jxl": 100000, "lxn": 100000, "bj": 200000}
# 2. 添加(字典不存在该key)
# 字典[键] = 值
if "ss" not in dict01:
dict01["ss"] = 700000
定位
dict01 = {"jxl": 100000, "lxn": 100000, "bj": 200000}
# 定位 字典名[键]
# -- 读取
print(dict01["lxn"])
# -- 修改
if "jxl" in dict01:
dict01["jxl"] = 500000
删除
dict01 = {"jxl": 100000, "lxn": 100000, "bj": 200000}
# 4. 删除 del 字典名[键]
del dict01["bj"]
遍历
dict01 = {"jxl": 100000, "lxn": 100000, "bj": 200000}
# -- 所有键
# for 键名 in 字典:
for key in dict01:
print(key)
# -- 所有值
# for 值名 in 字典.values():
for value in dict01.values():
print(value)
# -- 所有键和值
# for 键,值 in 字典.items():
# 不建议
# for item in dict01.items():
# print(item[0])
# print(item[1])
for key, value in dict01.items():
print(key)
print(value)
字典推导式
定义:
使用简易方法,将可迭代对象转换为字典。
# 练习1:将两个列表,合并为一个字典
# 姓名列表["张无忌","赵敏","周芷若"]
# 房间列表[101,102,103]
# 练习2:颠倒练习1字典键值
list_names = ["张无忌", "赵敏", "周芷若"]
list_rooms = [101, 102, 103]
# dict_infos = {}
# for i in range(len(list_names)):
# # list_rooms[i] 作为key
# # list_names[i] 作为value
# dict_infos[list_rooms[i]] = list_names[i]
# print(dict_infos)
dict_infos = {list_rooms[i]: list_names[i]
for i in range(len(list_names))
}
result = {v: k for k, v in dict_infos.items()}
print(dict_infos)
print(result)
5、集合(set)
定义
由一系列不重复的不可变类型变量(元组/数/字符串)组成的可变散列容器。
相当于只有键没有值的字典(键则是集合的数据)。
基础操作
创建
价值1:可以去重
set01 = {"悟空", "八戒", "唐三藏"}
list01 = [10, 20, 30, 20, 30, 40]
set02 = set(list01)
print(set02)
添加
set01 = {"悟空", "八戒", "唐三藏"}
# 2. 添加 集合名.add(元素)
set01.add("jxl")
删除
set01 = {"悟空", "八戒", "唐三藏"}
# 3. 删除 集合名.remove(元素)
set01.remove("八戒")
遍历
# 4. 遍历
for item in set01:
print(item)
价值
- 去重
set01 = {"悟空", "八戒", "唐三藏"}
list01 = [10, 20, 30, 20, 30, 40]
set02 = set(list01)
print(set02)
- 数学运算
s1 = {1, 2, 3}
s2 = {2, 3, 4}
# 1. 交集&:返回共同元素。
s3 = s1 & s2 # {2, 3}
# 2. 并集:返回不重复元素
s4 = s1 | s2 # {1, 2, 3, 4}
# 3. 补集-:返回只属于其中之一的元素
s5 = s1 - s2 # {1} 属于s1但不属于s2
s6 = s2 - s1 # {4}
# 补集^:返回不同的的元素
s7 = s1 ^ s2 # {1, 4} 等同于(s1-s2 | s2-s1)
# 4. 子集 <:判断一个集合的所有元素是否完全在另一个集合中
# 5.超集 >:判断一个集合是否具有另一个集合的所有元素
s8 = {2, 3}
# s8 子集 s1 超集
print(s8 < s1) # True
print(s1 > s8) # True