- 基本数据类型
- 数字
- int()
- bit_lenght()
- 字符串
- capitalize()
- casefold()
- lower()
- swapcase()
- center()
- ljust()
- rjust()
- count()
- endswith()
- startswith()
- find()
- format()
- format_map()
- index()
- isalnum()
- isalpha()
- isdigit()
- isdecimal()
- isnumeric()
- isidentifier()
- isprintable()
- isspace()
- istitle()
- * join()
- expandtabs()
- islower()
- lower()
- isupper()
- upper()
- lstrip()
- rstrip()
- strip()
- maketrans()
- translate()
- partition()
- rpartition()
- split()
- rsplit()
- splitlines()
- startswith()
- endswith()
- swapcase()
- len()
- replace()
- range()
- 索引
- 切片
- 练习
- 列表 (有序,元素可以被修改)
- 索引
- 切片
- for,while循环
- in
- del
- 查找元素
- 数据类型转换
- 将字符串转换为列表
- 列表转换为字符串
- append()
- clear()
- copy()
- count()
- extend()
- index()
- insert()
- pop()
- remove()
- reverse()
- sort()
- 元组 (有序的)
- 取值
- 通过切片取值
- 通过索引取值
- 通过for循环取元组中的值
- 数据类型转换
- count()
- index()
- 元组中列表一级元素不可修改,但列表中的元素可被修改
- 取值
- 字典
- 获取字典的值
- del()
- for循环字典
- clear()
- copy()
- fromkeys()
- get()
- pop()
- popitem()
- setdefault()
- update()
- 集合
- 创建集合
- set()
- frozenset()
- add()
- clear
- copy()
- pop()
- remove()
- discard()
- intersection()
- union()
- difference()
- symmetric_difference()
- difference_update()
- symmetric_difference_update
- intersection_update()
- isdisjoint()
- issubset()
- issuperset()
- update()
- 字符串格式化
- 百分号格式
- format格式
- 数字
author:JevonWei
版权声明:原创作品
blog:http://119.23.52.191/
基本数据类型
可变数据 | 列表,字典 |
---|---|
不可变数据 | 字符串,数字,元组 |
数字
int()
-
将字符串转换为数字
a = '123'
b = int(a)
print(type(b),b)
123 -
将十六进制数转换为十进制输出
num = "0011"
v = int(num,base=16)
print(v)
17
bit_lenght()
-
当前数字的二进制,至少用N位表示
age = 5
r = age.bit_length()
print(r)
3
字符串
-
字符串一旦创建,不可修改
-
一旦修改或者拼接,都会造成重新生成字符串
-
常用的内置方法
join(),split(),find(),strip(),replace(),upper(),lower()
capitalize()
-
首字母大写
test = 'danran'
v = test.capitalize()
print(v)
Danran
casefold()
lower()
-
字符串变小写,casefold比lower效果更好
test = 'DanRan'
v1 = test.casefold()
print(v1)
v2 = test.lower()
print(v2)
danran
danran
swapcase()
-
字符串中的字母大小写转换
test = "DanRan"
v = test.swapcase()
print(v)
dANrAN
center()
-
设置字符串宽度,并将内容居中,20代表总长度,* 空白未知填充
test = "Danran"
v = test.center(10)
print(v)
v1 = test.center(15,"*")
print(v1)
Danran
*Danran
ljust()
-
设置字符串的长度,不足的位数以指定字符填充,字符串放在左边
test = "Danran"
v = test.ljust(20,'*')
print(v)
Danran**************
rjust()
-
设置字符串的长度,不足的位数以指定字符填充,字符串放在右边
test = "Danran"
v = test.rjust(20,'*')
print(v)
**************Danran
count()
-
在字符串中寻找子序列出现的次数
test = "DanranJevonDan"
v = test.count('e')
print(v)
v1 = test.count('a') # 在test字符串中查找‘a’子字符串出现的次数
print(v1)
v1 = test.count('a',5) # 在test字符串中从第五个字符开始查找‘a’子字符串出现的次数
print(v1)
v1 = test.count('a',5,7) # 在test字符串中从第五个字符到第七个字符直间查找‘a’子字符串出现的次数
print(v1)
1
3
1
0
endswith()
-
判断字符串是否是以指定字符结尾
test = "danran"
v = test.endswith("a")
print(v)
v = test.endswith("n")
print(v)
False
True
startswith()
-
判断字符串是否以指定字符开始
test = "danran"
v = test.startswith("d")
print(v)
True
find()
-
从开始往后找,找到第一个之后,获取其位置
test = "danran"
v = test.find("an")
print(v)
1
v = test.find("an",3,5)
print(v)
v = test.find("an",3,6)
print(v)
-1
4
format()
-
格式化字符串,将字符串中的占位符替换为指定的值
test = 'I am {name},age {age}'
print(test)
v = test.format(name='danran',age=18)
print(v)
I am {name},age {age}
I am danran,age 18test = 'I am {0},age {1}' # 按参数的位置传递相应的值
print(test)
v = test.format('danran',18)
print(v)
I am {name},age {age}
I am danran,age 18
format_map()
-
格式化,传入的值为字典格式
test = 'I am {name},age {age}'
v1 = test.format(name='df',age=10)
v2 = test.format_map({"name":'danran','age':10})
print(v1)
print(v2)
index()
-
在字符串中寻找子字符串出现的位置,若在字符串中找不到子序列,则报错
test = "danran"
v = test.index('n')
print(v)
v = test.index('q') # 找不到此字符‘q’,则输出报错
print(v)
Traceback (most recent call last):
File "D:/Project/test.py", line 55, in
v = test.index('q')
ValueError: substring not found
2
isalnum()
-
判断字符串中是否仅包含字符和数字
test = "danra123_"
v = test.isalnum()
print(v)
test = "danra123"
v = test.isalnum()
print(v)
False
True
isalpha()
-
判断字符串中是否全为字母
test = "danran12"
v = test.isalpha()
print(v)test = "danran"
v = test.isalpha()
print(v)
False
True
isdigit()
isdecimal()
isnumeric()
-
判断字符串中是否全为数字,isdigit()比isdecimal()功能强大,isnumeric()还支持中文数字
test = "123456"
v1 = test.isdecimal()
v2 = test.isdigit()
print(v1,v2)
True Truetest = "②"
v1 = test.isdecimal()
v2 = test.isdigit()
print(v1,v2)
False Truetest = "二"
v1 = test.isdecimal()
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1,v2,v3)
False
False
True
isidentifier()
-
判断字符串是否是合法的标识符,标识符由数字、字母及下划线组成,且不能以数字开头
a = "_123"
v = a.isidentifier()
print(v)
Truea = "123"
v = a.isidentifier()
print(v)
False
isprintable()
-
判断是否存在不可显示的字符,eg:/t,/n
test = "danran"
v = test.isprintable()
print(v)
Truetest = "dan\nZran"
v = test.isprintable()
print(v)
False
isspace()
-
判断字符串是否全为空格
test = "danran"
v = test.isspace()
print(v)
Falsetest = " dan ran"
v = test.isspace()
print(v)
Falsetest = " " # test字符串全为空,即空字符串
v = test.isspace()
print(v)
True
istitle()
-
将字符串修改为标题格式
test = "Dan ran Jevon Wei is me"
v = test.istitle()
print(v)
Falsetest = "Dan ran Jevon Wei is me"
v1 = test.title() # 将test字符串的所有字符转换为首字母大写,即标题格式
print(v1)
v2 = v1.istitle() # v1即为标题,输出为True
print(v2)
Dan Ran Jevon Wei Is Me
True
* join()
-
将字符串中的每个元素按照指定分隔符进行拼接
test = "DanranJevonWei"
print(test)
t = ' '
v1 = t.join(test)
v2 = "_".join(test)
print(v1)
print(v2)
D a n r a n J e v o n W e i
D_a_n_r_a_n_J_e_v_o_n_W_e_i
expandtabs()
-
断句,即按指定字段分割
test = "username\temail\tpasswd\ndanran\[email protected]\tPass1234\ndanran\[email protected]\tPass1234\ndanran\[email protected]\tPass1234"
v = test.expandtabs(15) # \t制表符,每15位为一段,不足的以制表符填充
print(v)
username email passwd
danran [email protected] Pass1234
danran [email protected] Pass1234
danran [email protected] Pass1234
islower()
- 判断字符串是否全为小写字符
lower()
-
将字符串转换为小写
test = "DanRan"
v1 = test.islower()
v2 = test.lower()
print(v1)
print(v2)
False
danran
isupper()
- 判断字符串是否全为大写
upper()
-
将字符串转换为大写
test = "DanRan"
v1 = test.isupper()
v2 = test.upper()
print(v1)
print(v2)
False
DANRAN
lstrip()
- 去除字符串左边指定的字符,默认去除空格,制表符及上边空白行
rstrip()
- 去除字符串右边指定的字符,默认去除空格,制表符及下边空白行
strip()
-
去除字符串两边指定的字符,默认去除空格,制表符及两边空白行
test = " Danran "
v1 = test.lstrip()
v2 = test.rstrip()
v3 = test.strip()
print(v1)
print(v2)
print(v3)
Danran
Danran
Danrantest = "Danran\n "
v1 = test.lstrip('Da') # 去除字符串左边的“Da”字符
v2 = test.rstrip() # 去除字符串右边的空格,制表符及空白行
v3 = test.strip()
print(v1)
print(v2)
print(v3)
nran # v1
# v1
Danran # v2
Danran # v3
maketrans()
- 构建一个字符的对应关系
translate()
-
根据指定的字符对应关系将字符串进行转换
test = "Wei are you Dan ran on" # 定义一个字符串
v = str.maketrans("Danran","123456") # 构建一个字符对应关系,Danran对应为123345
new_test = test.translate(v) # 将test字符串按照构建的v字符对应关系进行转换
print(new_test)
Wei 54e you 156 456 o6
partition()
- 将字符串按指定字符进行分割,仅分割为三份,从左边开始分割
rpartition()
- 将字符串按指定字符进行分割,仅分割为三份,从右边开始分割
split()
- 将字符串按指定字符串进行分割,可指定查找到的分割符的次数,不输出分隔符
rsplit()
-
将字符串按指定字符串进行分割,可指定查找到的分割符的次数,不输出分隔符
test = "Danrandanrandanran" # 定义一个字符串
v1 = test.partition("a") # 将字符串以‘a’字符分割为三份,从左边开始分割
v2 = test.rpartition("a") # 将字符串以‘a’字符分割为三份,从右边开始分割
print(v1)
print(v2)
('D', 'a', 'nrandanrandanran')
('Danrandanrandanr', 'a', 'n')test = "Danrandanrandanran" # 定义一个字符串
v1 = test.split("a",2) # 将字符串以‘a’字符分割,从左边开始查找两个‘a’分割字符,拿不到'a'分隔符
v2 = test.rsplit("a",2) # 将字符串以‘a’字符分割,从右边开始查找两个‘a’分割字符,拿不到'a'分隔符
print(v1)
print(v2)
['D', 'nr', 'ndanrandanran']
['Danrandanrand', 'nr', 'n']
splitlines()
-
根据换行符分割字符串,参数为True时保留换行符,参数为False是移除换行符
test = "Dan\nran\n Jevo\nnan"
v = test.splitlines(True) # 参数为True,保留换行符
print(v)
['Dan\n', 'ran\n', ' Jevo\n', 'nan']test = "Dan\nran\n Jevo\nnan"
v = test.splitlines(False) # 参数为False,移除换行符
print(v)
['Dan', 'ran', ' Jevo', 'nan']
startswith()
- 判断字符串是否以指定的字符开头
endswith()
-
判断字符串是否以指定的字符结尾
test = "backend 1.1.2.3"
v = test.startswith('a')
print(v)
Falsetest = "backend 1.1.2.3"
v = test.endswith('3')
print(v)
True
swapcase()
-
指定字符串大小写转换
test = 'DanRan'
v = test.swapcase()
print(v)
dANrAN
len()
-
获取当前字符串的长度
test = "Danran"
v=len(test)
print(v)
6
replace()
-
查找字符串中的特定字符并替换
test = "Danran"
v=test.replace('a','AAA')
print(v)
DAAAnrAAAntest = "Danranaev"
v=test.replace('a','AAA',2) # 只替换前两个匹配的字符
print(v)
DAAAnrAAAnaev
range()
-
帮忙创建连续的数字。
v = range(100)
for item in v:
print(item)v = range(0,100,5) # 间隔为5,输出0-100的数字
for item in v:
print(item)
索引
-
通过下标获取字符串中的某一个字符
test = "alex"
v=test[2]
print(v)
e
切片
-
将字符串进行切片
test="alex"
v=test[0:2]
print(v)
altest="alex"
v=test[0:-1]
print(v)
ale
练习
-
将文件对应的索引打印出来
test = input(">>>")
print(test)
l = len(test)
print(l)r = range(0,l)
for item in r:
print(item,test[item])
danran # print(test)
6 # print(l),打印test字符串的长度
0 d
1 a
2 n
3 r
4 a
5 n
列表 (有序,元素可以被修改)
- 中括号括起来
- 使用“,”分割每个元素
- 列表中的元素可以是数字,字符串,列表,布尔值...
- “集合”内部可嵌套任何类型
索引
索引取值
li = [1,2,3,"age",["Jevon","danran",44],1,5,7]
print(li[4]) # 通过索引取列表中索引位置为4的值
print(li[3:6]) # 通过索引取列表中索引位置为3到6的值
['Jevon', 'danran', 44]
['age', ['Jevon', 'danran', 44], 1]
通过索引修改列表元素
li = [1,2,3,"age",["Jevon","danran",44],1,5,7]
li[1] = 100
print(li)
[1, 100, 3, 'age', ['Jevon', 'danran', 44], 1, 5, 7]
li = [1,2,3,"age",["Jevon","danran",44],1,5,7]
li[1] = [100,99,98]
print(li[1])
[100, 99, 98]
切片
通过切片修改列表元素
li = [1,2,3,"age",["Jevon","danran",44],1,5,7]
li[1:3] = [100,90] # 通过切片修改列表的第1到3位元素
print(li)
[1, 100, 90, 'age', ['Jevon', 'danran', 44], 1, 5, 7]
for,while循环
for 循环取列表中的每个值
li = [1,2,3,"age",["Jevon","danran",44],1,5,7]
for item in li:
print(item)
1
2
3
age
['Jevon', 'danran', 44]
1
5
7
in
-
判断指定元素是否包含在列表中
li = [1,2,3,"age",["Jevon","danran",44],1,5,7]
v = 'age' in li
print(v)
Trueli = [1,2,3,"age",["Jevon","danran",44],1,5,7]
v = 'danran' in li
print(v)
Falseli = [1,2,3,"age",["Jevon","danran",44],1,5,7]
v = 'danran' in li[4]
print(v)
True
del
删除列表元素
li = [1,2,3,"age",["Jevon","danran",44],1,5,7]
del li[0] # 删除列表的第“0”位元素
print(li)
[2, 3, 'age', ['Jevon', 'danran', 44], 1, 5, 7]
删除列表多个元素
li = [1,2,3,"age",["Jevon","danran",44],1,5,7]
del li[1:3] # 通过切片删除列表的第1到3个元素
print(li)
[1, 'age', ['Jevon', 'danran', 44], 1, 5, 7]
查找元素
查找子列表中的元素
li = [1,2,3,"age",["Jevon","danran",[44,22]],1,5,7]
print(li[4][2][1]) # 查找列表中的22元素
22
数据类型转换
将字符串转换为列表
-
内部通过调用for循环实现
s = "asdfqwertaffzxcvvbgfssaa"
new_li = list(s) # 将字符串s转换为列表
print(new_li)
['a', 's', 'd', 'f', 'q', 'w', 'e', 'r', 't', 'a', 'f', 'f', 'z', 'x', 'c', 'v', 'v', 'b', 'g', 'f', 's', 's', 'a', 'a']
列表转换为字符串
若字符串为纯字符组成
li = ["12","age","danran"]
v = "".join(li) # 将每个字符间以“”分割
print(v)
12agedanran
若字符串由字符和数字组成,则需要通过for循环逐个处理
li = [11,22,33,"abc",'danran']
s = ""
for i in li:
s += str(i)
print(s)
112233abcdanran
append()
-
在列表的最后追加元素
li = [11,22,33,"abc",'danran']
li.append("Jevon")
print(li)
[11, 22, 33, 'abc', 'danran', 'Jevon']
clear()
-
清空列表
li = [11,22,33,"abc",'danran']
li.clear()
print(li)
[]
copy()
-
拷贝复制列表,浅拷贝
li = [11,22,33,"abc",'danran']
v = li.copy()
print(v)
[11, 22, 33, 'abc', 'danran']
count()
-
统计指定元素在列表中出现的次数
li = [11,2,33,"abc",'danran',2] v = li.count(2) print(v) 2
extend()
-
将指定的元素或子列表扩展到原列表中,参数为可迭代对象
li = [11,2,33,"abc",'danran',2]
li.extend("Jevon")
print(li)
[11, 2, 33, 'abc', 'danran', 2, 'J', 'e', 'v', 'o', 'n']li = [11,2,33,"abc",'danran',2]
li.extend([999,"Jevon"])
print(li)
[11, 2, 33, 'abc', 'danran', 2, 999, 'Jevon']比较append()
li = [11,2,33,"abc",'danran',2]
li.append([999,"Jevon"])
print(li)
[11, 2, 33, 'abc', 'danran', 2, [999, 'Jevon']]
index()
-
根据指定的值获取当前值的索引位置,默认从左向右查找,可指定查找的起始及终止位置
li = [11,2,33,"abc",'danran',2]
v = li.index(2) # 默认从前向后查找
print(v)
1li = [11,2,33,"abc",'danran',2]
v = li.index(2,2) # 指定查找的起始位置为2
print(v)
5
insert()
-
在列表的指定索引位置插入元素
li = [11,2,33,"abc",'danran',2]
li.insert(1,"Jevon") # 在列表的第1索引位置插入元素“Jevon”
print(li)
[11, 'Jevon', 2, 33, 'abc', 'danran', 2]
pop()
-
删除列表指定索引位置的元素,并可获取删除的值,默认删除删除最后的元素
li = [11,2,33,"abc",'danran',2]
v = li.pop() # 删除列表最后以为元素
print(v) # 返回删除的元素
print(li)
2
[11, 2, 33, 'abc', 'danran']删除列表索引位置为3的元素
li = [11,2,33,"abc",'danran',2]
v = li.pop(3)
print(v)
print(li)
abc
[11, 2, 33, 'danran', 2]
remove()
-
删除列表指定元素,左边优先
li = [11,2,33,"abc",'danran',2]
li.remove(33) # 删除列表中的元素“33”
print(li)
[11, 2, 'abc', 'danran', 2]
reverse()
-
将当前列表进行反转
li = [11,2,33,"abc",'danran',2]
li.reverse()
print(li)
[2, 'danran', 'abc', 33, 2, 11]
sort()
-
将列表中的元素排序
从小到大排序
li = [11,22,44,11,2,77]
li.sort()
print(li)
[2, 11, 11, 22, 44, 77]从大到小排序
li = [11,22,44,11,2,77]
li.sort(reverse=True)
print(li)
[77, 44, 22, 11, 11, 2]
元组 (有序的)
-
习惯写元组的时候,随后在最后加入“,”
-
元素不可被修改,不能被删除或增加
tu = (11,"danran",(1,2,3),[(33,44)],True,123,)
print(tu)
(11, 'danran', (1, 2, 3), [(33, 44)], True, 123)
取值
通过切片取值
tu = (11,"danran",(1,2,3),[(33,44)],True,123,)
v = tu[2:5]
print(v)
((1, 2, 3), [(33, 44)], True)
通过索引取值
tu = (11,"danran",(1,2,3),[(33,44)],True,123,)
v = tu[2]
print(v)
(1, 2, 3)
通过for循环取元组中的值
tu = (11,"danran",(1,2,3),[(33,44)],True,123,)
for item in tu:
print(item)
11
danran
(1, 2, 3)
[(33, 44)]
True
123
数据类型转换
-
将字符串,列表,元组互相转换
s = "asdfghj124" li = ["danran",123,[11,23]] tu = (123,123,"danran") 将字符串s转换Wie元组 v = tuple(s) print(v) ('a', 's', 'd', 'f', 'g', 'h', 'j', '1', '2', '4') 将列表转换为元组 v = tuple(li) print(v) ('danran', 123, [11, 23]) 将元组转换为列表 v = list(tu) print(v) [123, 123, 'danran'] 将元组转换Wie字符串 v = str(tu) print(v) print(type(v)) (123, 123, 'danran')
count()
-
统计指定元素在元组中出现的次数
tu = (11,"danran",(11,22,3),11,True,)
v = tu.count(11)
print(v)
2
index()
-
返回指定元素在元组中的索引位置,左边优先
tu = (11,"danran",(11,22,3),11,True,)
v = tu.index(11)
print(v)
0
元组中列表一级元素不可修改,但列表中的元素可被修改
tu = (11,"danran",(11,22,3),[(12,23,34),12],True,)
tu[3][1] = 1111
print(tu)
(11, 'danran', (11, 22, 3), [(12, 23, 34), 1111], True)
字典
-
字典的value可以是任何值
-
列表、字典不能作为字典的key
-
字典是无序的
info = {"k1":"12","k2":"23"}
print(info)
{'k1': '12', 'k2': '23'}
获取字典的值
info = {"k1":"12","k2":"23"}
v = info["k2"]
print(v)
23
del()
-
字典支持del删除
info = {"k1":"12","k2":"23"}
del info["k1"]
print(info)
{'k2': '23'}
for循环字典
默认for循环只返回key键
info = {"k1":"12","k2":"23"}
for item in info:
print(item)
k1
k2
for循环返回字典的key键
info = {"k1":"12","k2":"23"}
for item in info.keys():
print(item)
k1
k2
for循环获取字典的value值
info = {"k1":"12","k2":"23"}
for item in info.values():
print(item)
12
23
for同时返回字典的key键和value值
info = {"k1":"12","k2":"23"}
for k,v in info.items():
print(k,v)
k1 12
k2 23
clear()
- 清空元组
copy()
- 复制拷贝元组
fromkeys()
-
根据序列创建字典,并指定统一的值
v = dict.fromkeys(["k1",12,"danran"])
print(v)
{'k1': None, 12: None, 'danran': None}v = dict.fromkeys(["k1",12,"danran"],"Jevon")
print(v)
{'k1': 'Jevon', 12: 'Jevon', 'danran': 'Jevon'}
get()
-
根据字典的键取值
info = {"k1":"12","k2":"23"}
v = info.get("k1")
print(v)
12若指定的键不存在,则返回None,并将默认值赋给此不存在的键
info = {"k1":"12","k2":"23"}
v = info.get("k1111","danran")
print(v)
Noneinfo = {"k1":"12","k2":"23"}
v = info.get("k1111","danran")
print(v)
danran
pop()
-
删除字典中的键值对,当删除的键值对不存在时,可返还默认值
info = {"k1":"12","k2":"23"}
v = info.pop("k1") # 删除k1键值对
print(info)
print(v) # 将删除的k1键值对打印出来
{'k2': '23'}
12删除的键值对k1111不存在,则返回“danran”
info = {"k1":"12","k2":"23"}
v = info.pop("k1111","danran")
print(info)
print(v)
{'k1': '12', 'k2': '23'}
danran
popitem()
- 随机删除键值对,并返回删除的键值对
info = {"k1":"12","k2":"23"}
v = info.popitem()
print(info)
print(v)
{'k1': '12'}
('k2', '23')
setdefault()
-
设置键的默认值,若键值对已存在,则获取当前key对应的值,若key不存在,则设置此key对应的值为指定的值
info = {"k1":"12","k2":"23"}
v = info.setdefault("k111","12334")
print(info)
print(v)
{'k1': '12', 'k2': '23', 'k111': '12334'}
12334
update()
-
更新字典,如key存在,则更新相应的键值,若key不存在,则增加相应的键值对
info = {"k1":"12","k2":"23"}
info.update({"k1":"1234","k3":"11"})
print(info)
{'k1': '1234', 'k2': '23', 'k3': '11'}
集合
- 集合为无序的,且为不可变数据类型。
- 定义:由不同元素组成的集合,集合中是一组无序排列的可hash值,可作为字典的key,即必须为不可变类型。
- 特性:
集合的目的是将不同的值存放在一起,不同的集合间用来做关系运算,无须纠结于集合中单个值。
创建集合
s = {1,2,3,4,5}
print(type(s))
set()
-
创建集合
s = set("danran")
print(s)
{'a', 'r', 'd', 'n'}
frozenset()
-
创建不可变集合
s = frozenset("danrna")
print(s)
frozenset({'n', 'd', 'a', 'r'})
add()
-
集合中添加元素
s = {1,2,3,4,5}
s.add("danran")
print(s)
{1, 2, 3, 4, 5, 'danran'}
clear
-
清空集合
s = {1,2,3,4,5}
s.clear()
print(s)
set()
copy()
-
集合拷贝复制
s = {1,2,3,4,5}
a = s.copy()
print(a)
{1, 2, 3, 4, 5}
pop()
-
随机删除集合中的元素
s = {1,2,3,4,5}
s.pop()
print(s)
{2, 3, 4, 5}
remove()
-
删除集合中的指定元素,若指定元素不存在,则报错
s = {1,2,3,4,5,"danran"}
s.remove("danran") # 删除集合中的元素“danran”
print(s)
{1, 2, 3, 4, 5}
discard()
-
删除集合中的指定元素,若指定元素不存在,则不返回报错信息
s = {1,2,3,4,5,"danran"}
s.discard("danran")
print(s)
{1, 2, 3, 4, 5}
intersection()
-
两个集合的交集,等同于"&".
p = set(["danran","Jevon","Wei","Jevon"])
l = set(["dan","Wei"])
print(p,l)
print(p.intersection(l)) # intersection()等用于"&".
print(p&l)
{'Jevon', 'danran', 'Wei'} {'dan', 'Wei'}
{'Wei'}
{'Wei'}
union()
-
两个集合的并集,等同于"|"。
p = set(["danran","Jevon","Wei","Jevon"])
l = set(["dan","Wei"])
print(p,l)
print(p.union(l)) # 输出两个集合的并集,等同于"|"符合。
print(p|l)
{'Jevon', 'danran', 'Wei'} {'dan', 'Wei'}
{'dan', 'Jevon', 'danran', 'Wei'}
{'dan', 'Jevon', 'danran', 'Wei'}
difference()
-
两个集合的差集,等同于"-"符号。
p = set(["danran","Jevon","Wei","Jevon"])
l = set(["dan","Wei"])
print(p,l) # 打印两个集合
print(p.difference(l)) # 集合p于集合l的差集
print(p-l) # 集合p于集合l的差集
print(l.difference(p)) # 集合l于集合p的差集
print(l-p) # 集合l于集合p的差集
{'danran', 'Jevon', 'Wei'} {'Wei', 'dan'} # 打印两个集合
{'danran', 'Jevon'} # 集合p于集合l的差集
{'danran', 'Jevon'} # 集合p于集合l的差集
{'dan'} # 集合l于集合p的差集
{'dan'} # 集合l于集合p的差集
symmetric_difference()
-
两个集合的交叉补集运算,等同于"^"符号
p = set(["danran","Jevon","Wei","Jevon"])
l = set(["dan","Wei"])
print(p,l)
print(p.symmetric_difference(l)) # p集合于l集合的交叉补集
print(p^l) # p集合于l集合的交叉补集
print(l.symmetric_difference(p)) # l集合于p集合的交叉补集
print(l^p) # l集合于p集合的交叉补集
{'Wei', 'Jevon', 'danran'} {'Wei', 'dan'}
{'danran', 'Jevon', 'dan'} # p集合于l集合的交叉补集
{'danran', 'Jevon', 'dan'} # p集合于l集合的交叉补集
{'Jevon', 'danran', 'dan'} # l集合于p集合的交叉补集
{'Jevon', 'danran', 'dan'} # l集合于p集合的交叉补集
difference_update()
-
更新两个集合的差集
p = set(["danran","Jevon","Wei","Jevon"])
l = set(["dan","Wei"])
print(p,l)
s = p.difference(l)
print(s)
p.difference_update(l) # 更新p集合与l集合的差集
print(s)
{'Wei', 'Jevon', 'danran'} {'dan', 'Wei'}
{'Jevon', 'danran'}
{'Jevon', 'danran'} # 更新后的差集
symmetric_difference_update
-
更新两个集合的交叉补集
p = set(["danran","Jevon","Wei","Jevon"])
l = set(["dan","Wei"])
print(p,l)
s = p.symmetric_difference(l)
print(s)
p.symmetric_difference_update(l) # 更新两个集合的交叉补集
print(s)
{'danran', 'Wei', 'Jevon'} {'Wei', 'dan'}
{'danran', 'Jevon', 'dan'}
{'danran', 'Jevon', 'dan'} # 输出更新后的两个集合的交叉补集
intersection_update()
- 更新两个集合的交集
isdisjoint()
-
如果两个集合没有交集,即返回"True"
p = set(["danran","Jevon","Wei","Jevon"])
l = set(["dan","Wei"])
q = set(["li","ran"])
print(p.isdisjoint(l)) # 集合p和集合l交集不为空,即返回False
print(l.isdisjoint(q)) # 集合l和集合q交集为空,即返回True
False
True
issubset()
-
判断集合是否是子集关系,等同于"l<=p"符号。
p = set(["danran","Wei","Jevon"])
l = set(["danran","Wei"])
print(l.issubset(p)) # 集合l是集合p的子集
print(l<=p)
print(p.issubset(l))
True
True
False
issuperset()
-
判断集合是否是父集关系,等同于"p>=l"符号。
p = set(["danran","Wei","Jevon"])
l = set(["danran","Wei"])
print(p.issuperset(l)) # 集合p是集合l的父集
print(p>=l)
True
True
update()
-
对集合更新多个值
p = set(["danran","Wei","Jevon"])
l = set(["danran","xin"])
p.update(l)
print(p)
{'Wei', 'danran', 'Jevon', 'xin'}对可迭代的数据都可更新到集合中
p = set(["danran","Wei","Jevon"])
p.update(["dan"])
print(p)
{'dan', 'Wei', 'danran', 'Jevon'}
字符串格式化
- 百分号格式
- format格式
百分号格式
%[(name)][flags][width].[precision]typecode
- (name) # 可选,用于选择指定的key
- flags # 可选,可供选择的值由
+ 右对齐:正数前加正号,负数前加负号
- 左对齐:正数前无符号,负数前加负号
空格 右对齐:正数前加空格,负数前加负号
0 右对齐:正数前无符号,负数前加负号;用0填充空白处
- width # 可选,占有宽度
- .precision # 可选,小数点后保留的位数
- typecode # 必选