Python学习(五)—— 集合和字符格式化
2024-09-03 20:39:48
数据类型和变量的总结
字符串
数字
列表
元组
字典
分类
1、可变不可变:
可变(即修改变量值以后id不改变):列表、字典
不可变(即修改变量值以后id改变):字符串、数字、元组
2、访问顺序:
直接访问:数字
顺序访问:字符串、列表、元组
映射访问:字典
3、存放元素个数:
容器类型:列表、元组、字典
原子类型:数字、字符串
集合
定义:由不同元素组成的集合,集合中是一组无序排列的可hash值(元素必须都是不可变类型:字符串、数字、元组),可以作为字典的key
特点:集合的目的是将不同值存放在一起,不同的集合间用","分开
s = {1,3,5,2,6,5}
print(s)
#{1, 2, 3, 5, 6}
for i in s:
print(i)
"""
1
2
3
5
6
"""
s1 = set("alexx")
print(s1)
#{'l', 'a', 'x', 'e'}
注意set()定义的使用方式:
s2 = set(["alex","alex","sb"])
#s2 = set("alex","alex","sb")=>Wrong
print(s2)
#{'alex', 'sb'}
注意:另一种定义方式可以将集合定义为不可变类型
s = frozenset("hello")
print(s)
#frozenset({'e', 'o', 'l', 'h'})
集合的运算关系
.intersection() &
求交集
1 English = ["Lily", "Sally", "Tom", "Paul"]
2 Math = ["Lily", "Sally", "John"]
3 English_s = set(English)
4 Math_s = set(Math)
5 print(English_s, Math_s)
6 print(English_s.intersection(Math_s))
7 """
8 {'Sally', 'Paul', 'Lily', 'Tom'} {'Sally', 'Lily', 'John'}
9 {'Sally', 'Lily'}
10 """
.union() |
求并集
1 English = ["Lily", "Sally", "Tom", "Paul"]
2 Math = ["Lily", "Sally", "John"]
3 English_s = set(English)
4 Math_s = set(Math)
5 print(English_s|Math_s)
6 """
7 {'Sally', 'Paul', 'John', 'Tom', 'Lily'}
8 """
.difference() -
求差集
1 Math = ["Lily", "Sally", "John"]
2 English_s = set(English)
3 Math_s = set(Math)
4 print(English_s-Math_s)
5 print(Math_s-English_s)
6 """
7 {'Paul', 'Tom'}
8 {'John'}
9 """
.symmetric_difference() ^
求交叉补集(就是把各个集合独有的元素都取出来)
English = ["Lily", "Sally", "Tom", "Paul"]
Math = ["Lily", "Sally", "John"]
English_s = set(English)
Math_s = set(Math)
print(English_s.symmetric_difference(Math_s))
"""
{'John', 'Tom', 'Paul'}
"""
集合的类中功能
.add()
添加元素
.clear()
清空
.copy()
拷贝
.pop() .remove(x) .discard(x)
第一个任意删除,第二个删除指定的x,若不存在则报错,第三个删除不存在元素不报错
s = {1,3,5,2,6,5,"sad"}
s.pop()
print(s)
#{2, 3, 5, 6, 'sad'}
s.remove(5)
print(s)
#{2, 3, 6, 'sad'}
s.discard("sadddd")
print(s)
.difference_update()
求完差集后更新
English = ["Lily", "Sally", "Tom", "Paul"]
Math = ["Lily", "Sally", "John"]
English_s = set(English)
Math_s = set(Math)
English_s.difference_update(Math_s)
print(English_s)
#{'Paul', 'Tom'}
.isdisjoint()
判断两个集合是否没有交集,没有则返回True,有则返回False
s1 = {1,2,3}
s2 = {2,3,4}
print(s1.isdisjoint(s2))
#False
s1.issubset(s2)
判断s1是否是s2的子集
s1 = {1,2,3}
s2 = {2,3}
print(s2.issubset(s1))
#True
s1.issuperset(s2)
判断s1是否是s2的父集
s1 = {1,2,3}
s2 = {2,3}
print(s1.issuperset(s2))
#True
s1.update(s2)
将s1更新为s2
s1 = {1,2,3}
s2 = {2,3}
s1.update(s2)
print(s1)
#{1, 2, 3}
.add()
与update区别在于该功能只能添加一个值,而update不限
s1 = {1,2,3}
s1.add(5)
print(s1)
#{1, 2, 3, 5}
s1 = {1,2,3}
s1.update({5,6})
print(s1)
#{1, 2, 3, 5, 6}
字符串格式化
%s
msg = "I am %s and I like eating"%"Jenny" #%是固定格式,s代表是字符串形式
print(msg)
#I am Jenny and I like eating
info = "I am %s and I like %s" %("Kit", "dancing")
print(info)
#I am Kit and I like dancing
注意:虽然是%s,但其实可以接受任何类型
但是使用%d,只能接受数字类型
%(key)s
可以使用key的形式来拼接值
%.xf
打印x位浮点数
perct = 95.76778978757875378538
msg = "the percent is %.2f"%perct
print(msg)
#the percent is 95.77
%.xf %%
打印百分比
perct = 95.76778978757875378538
msg = "the percent is %.2f %%"%perct
print(msg)
#the percent is 95.77 %
.format() 格式化
空的{}来传值,不一一对应则报错
{0},{1},{2}...可以打乱顺序重新对应
字典形式
可以用字典形式传值,前面加**
可以用元组形式传值,前面加*
s1 = "I am {}, I hate {}".format("Kit","sleeping")
print(s1)
#I am Kit, I hate sleeping
s2 = "I am {0}, I like{2} but hate {1}".format("Kit","sleeping","eating")
print(s2)
#I am Kit, I likeeating but hate sleeping
s3 = "I am {name}, I like {hobby}, but hate {fruit}".format(name="Kit",hobby="music",fruit="banana")
print(s3)
#I am Kit, I like music, but hate banana
s4 = "I am {name}, I like {hobby}, but hate {fruit}".format(**{"name":"Kit","hobby":"music","fruit":"banana"})
print(s4)
#I am Kit, I like music, but hate banana
s5 = "I am {}, I like {}, but hate {}".format(*("Kit","music","banana"))
print(s5)
#I am Kit, I like music, but hate banana
s6 = "I am {:s}, my age is {:d}, and my score is {:%}".format(*("Kit",18,0.15))
print(s6)
#I am Kit, my age is 18, and my score is 15.000000%
最新文章
- python开启简单webserver
- iOS 判断内容是否是中文,两种实现
- Chrome离线下载地址
- [Angular2 Router] Understand the Angular 2 Base href Requirement
- 在MacOSX下用管理员权限打开App应用程序
- 多次读取请求request里数据
- Android环境结构--安装Eclipse错
- Java中的值传递
- VMware Workstation 12 Pro 之安装林耐斯-Elementaryos-系统
- 关于手残,搞废我的OLED屏幕的 追悼会
- [Awson原创]洪水(flood)
- git 本地提交代码到 github 远程库,没有弹框 github login
- IDEA中MAVEN项目Dependency not found 问题
- 在CentOS中部署.Net Core2.1网站
- 项目总结14:Windows远程连接redis(cmd指令或PowerShell指令)
- [Zlib]_[初级]_[使用zlib库压缩和解压STL string]
- 一个java版本的简单邮箱小爬虫
- php生成随机数
- BPM与ESB
- Go语言的包管理