1.字符串操作
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
>>> name = ("my name is cc")#首字母大写>>> print(name.capitalize())My name is cc>>> print(name.count('c'))#统计'c'的个数2>>>print(name.center(50,"-"))#输出50个字符,名字居中,两边补齐--------------------my name is cc------------------->>> print(name.endswith("-"))#判断是否以"-"结尾False>>> name = ("my name is cc")>>> print(name.expandtabs())#把字符串中的 tab 符号(' ')转为空格,tab 符号(' ')默认的空格数是 8。my name is cc>>> print(name.isdigit())#判断是否为整数False>>> name = ("my")>>> print(name.isidentifier())#判断是不是一个合法的标识符True>>> name = ('2my')>>> print(name.isidentifier())False>>> name = ("my")>>> print(name.islower())#是不是小写字符True>>> name = ("my")>>> print(name.isnumeric())#判断是不是纯数字False>>> name = ("my")>>> print(name.isspace()) #判断是不是空格False>>> name = ("My Name Is")>>> print(name.istitle())#判断是不是title(首字母都大写)True>>> name = ('MY')>>> print(name.isupper())#判断是否全是大写Truejoin() 拼接字符串,可指定拼接字符串,如:+>>> name = ('cc','mm','nn')>>> print('+'.join(name))cc+mm+nn>>> name = ('cc')>>> print(name.ljust(50,'*'))#输出50个字符,cc放在最左边,右边以*补齐cc************************************************>>> name = ('cc')>>> print(name.rjust(50,'*'))#输出50个字符,cc放在最右边,左边以*补齐************************************************cc>>> name = ('MY')>>> print(name.lower()) #小写my>>> name = ('my')>>> print(name.upper()) #大写MY>>> name = (' yy ')>>> print(name.strip())#自动去掉左右的回车和空格yy>>> print(name.lstrip())#自动去掉左边的空格和回车yy >>> print(name.rstrip())#自动去掉右边的空格和回车 yymaketrans() 方法用于创建字符映射的转换表,对于接受两个参数的最简单的调用方式,第一个参数是字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标。注:两个字符串的长度必须相同,为一一对应的关系。>>> a='Hello!World!'>>> t = a.maketrans('l','a')>>> print(a.translate(t))Heaao!Worad!>>> name = "cc is uu">>> print(name.replace("c","L",1))#替换,1代表替换几次Lc is uu(translate是字符的一一映射. 每个字符只要出现都会被替换为对应的字符.replace是字符串替换, 字符串完整出现后被整体替换.replace的两个字符串参数长度可以不同.)>>> name = 'ccisuu'>>> print(name.rfind("u"))#从左往右找到最右边满足条件的下标并返回5>>> name = ('cc,nn')>>> print(name.split(','))#分割字符串,默认用空格分割['cc', 'nn']>>> name = ('cc
uu')>>> print(name.splitlines())按照换行分割['cc', 'uu']>>> name = 'cc'>>> print(name.swapcase())#大小写转换CC |
2.字典
字典是无序的,是另一种可变容器模型,且可存储任意类型对象。
字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示:
|
1
|
d = {key1 : value1, key2 : value2 } |
键必须是唯一的,但值则不必。
值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组。
2.1 取出元素
|
1
2
3
4
5
6
7
|
>>> dict = {'name':'cc','age':18}>>> print(dict['name'])cc>>> dict = {'name':'cc','age':18}>>> print(dict.get('age'))18 |
2.2 修改字典
|
1
2
3
4
5
6
7
|
>>> dict = {'name':'cc','age':18}>>> dict['name'] = 'dd'#元素替换,若存在则修改>>> print(dict){'name': 'dd', 'age': 18}>>> dict['sale'] = 'boy'#若不存在则新增>>> print(dict){'age': 18, 'name': 'cc', 'sale': 'boy'} |
2.2 删除字典元素
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
>>> dict = {'name':'cc','age':18}>>> del dict['name']#删除键是name的值>>> print(dict){'age': 18}>>> dict.pop('age')#删除键是age的值>>> print(dict){'name': 'cc'}>>> dict.popitem()#随机删除:字典本身是无序的>>> print(dict){'age': 18}或{'name','cc'}>>> dict.clear()#清空词典所有条目>>> print(dict){}>>> del dict#删除字典>>> print(dict) |
2.3 多级字典嵌套及操作
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
>>> user = { "seller":{ "user1":"cc", "user2":"uu", "user3":"tt" }, "buyer":{ "tel1":101, "tel2":102, "tel3":103 }}>>> print(user){'buyer': {'tel2': 102, 'tel1': 101, 'tel3': 103}, 'seller': {'user2': 'uu', 'user1': 'cc', 'user3': 'tt'}} |
2.4 字典循环
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#方法一:建议用这种方法>>> info = {"name":"cc","age":18}>>> for i in info:>>> print(i,info[i])name ccage 18#方法二:>>> info = {"name":"cc","age":18}#会先把dict转成list,数据里大时莫用>>> for k,v in info.items():>>> print(k,v)name ccage 18 |
2.5 其他
和list比较,dict有以下几个特点:
- 查找和插入的速度极快,不会随着key的增加而变慢;
- 需要占用大量的内存,内存浪费多。
而list相反:
- 查找和插入的时间随着元素的增加而增加;
- 占用空间小,浪费内存很少。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
#values>>> info.values()#取出字典的valuedict_values(['LongZe Luola', 'XiaoZe Maliya'])#keys>>> info.keys()#取出字典的keydict_keys(['stu1102', 'stu1103'])#setdefault如果键在字典中,返回这个键所对应的值。如果键不在字典中,向字典中插入这个键,并且以default为这个键的值,并返回default。default的默认值为None>>> info.setdefault("stu1106","Alex")'Alex'>>> info{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'}>>> info.setdefault("stu1102","龙泽萝拉")'LongZe Luola'>>> info{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'}#update 合并,有相同的值就替换;没有就新增>>> info{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'}>>> b = {1:2,3:4, "stu1102":"龙泽萝拉"}>>> info.update(b)>>> info{'stu1102': '龙泽萝拉', 1: 2, 3: 4, 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'}#items 字段转换成列表info.items()dict_items([('stu1102', '龙泽萝拉'), (1, 2), (3, 4), ('stu1103', 'XiaoZe Maliya'), ('stu1106', 'Alex')])#通过一个列表生成默认dict,初始化一个字典,共享一个内存地址>>> dict.fromkeys([1,2,3],'testd'){1: 'testd', 2: 'testd', 3: 'testd'} |