概念
- 线性表
线性表(简称表),是一种抽象的数学概念,是一组元素的序列的抽象,它由有穷个元素组成(0
个或任意个)
顺序表:使用一大块连续的内存顺序存储表中的元素,这样实现的表称为顺序表,或称连续表
在顺序表中,元素的关系使用顺序表的存储顺序自然地表示
链接表:在存储空间中将分散存储的元素链接起来,这种实现称为链接表,简称链表
列表如同地铁站排好的队伍,有序,可以插队、离队,可以索引。
链表如同操场上手拉手的小朋友,有序但排列随意。或者可以想象成一串带线的珠子,随意盘放在桌
上。也可以离队、插队,也可以索引。
对比体会一下,这两种数据结构的增删改查。
列表
- 构造方式
- [] list() list(interable)
- 查,lst = list(range(5))
- 根据索引进行查找 lst[2]
- lst.index(value) lst.count(value)
- len(lst)
- 删,原地修改,
- lst.remove(value),
- lst.pop(index) 默认尾部弹出
- lst.clear() 清空列表
lst = list(range(6,10))
print(lst.pop())
print(lst)
print(lst.remove(6))
print(lst)
lst.pop(1)
print(lst)
lst.clear()
print(lst)
9
[6, 7, 8]
None
[7, 8]
[7]
[]
- 增
- lst.insert(index)
- lst.append() 尾部追加
- lst.extend()
-
-
- 返回新列表
-
lst = list(range(6,10))
lst.insert(0, 1)
print(lst)
lst.append(5)
print(lst)
lst.extend(range(5))
print(lst)
[1, 6, 7, 8, 9]
[1, 6, 7, 8, 9, 5]
[1, 6, 7, 8, 9, 5, 0, 1, 2, 3, 4]
- 改
- lst[index] = value
- sort
- reverse
- copy
lst = list(range(10, 6, -1))
print(lst)
lst.sort()
print(lst)
lst.reverse()
print(lst)
lst.sort(reverse=False)
print(lst)
lst1 = lst.copy()
print(lst1)
lst1[1] = 1
print(lst, lst1)
[10, 9, 8, 7]
[7, 8, 9, 10]
[10, 9, 8, 7]
[7, 8, 9, 10]
[7, 8, 9, 10]
[7, 8, 9, 10] [7, 1, 9, 10]
字符串
- 初始化, 不可类型,所以其增删改都生成新的字符串或者其他类型
s1 = 'string'
s2 = "string2"
s3 = '''this's a "String" '''
s4 = 'hello \n magedu.com'
s5 = r"hello \n magedu.com"
s6 = 'c:\windows\nt'
s7 = R"c:\windows\nt"
s8 = 'c:\windows\\nt'
name = 'tom'; age = 20 # python代码写在一行,使用分号隔开,不推荐
s9 = f'{name}, {age}' # 3.6支持f前缀
sql = """select * from user where name='tom' """
- 查找
- str[]
- str.find方法 rfind 不会报错,找不到会返回-1
- str.index()方法 rindex
- len(str)
s = 'google.edu'
print(s.find('))
print(s.find('edu', 3))
print(s.find('edu', 4))
print(s.find('edu', 6, 9))
print(s.find('edu', 7, 20))
print(s.find('edu', 200))
- 链接
-
- 两个字符串相加返回新字符串
- join方法 返回新字符串
s = 'abcd'
s1 = ','.join(s)
print(s, s1)
- 分割
- split(sep=None, maxsplit=-1) -> list of strings
从左至右
sep 指定分割字符串,缺省的情况下空白字符串作为分隔符
maxsplit 指定分割的次数,-1 表示遍历整个字符串
立即返回列表
- rsplit(sep=None, maxsplit=-1) -> list of strings
从右向左开始切,但是输出的字符串字符不会反
sep 指定分割字符串,缺省的情况下空白字符串作为分隔符
maxsplit 指定分割的次数,-1 表示遍历整个字符串
立即返回列表
- splitlines([keepends]) -> list of strings
按照行来切分字符串
keepends 指的是是否保留行分隔符
行分隔符包括\n、\r\n、\r等
s = ','.join('abcd')
print(s.split(','))
print(s.split())
print(s.split(',', 2))
s1 = '\na b \tc\nd\n' # 注意下面3个切割的区别
print(s1.split())
print(s1.split(' '))
print(s1.split('\n'))
print(s1.split('b'))
print(s1.splitlines())
['a', 'b', 'c', 'd']
['a,b,c,d']
['a', 'b', 'c,d']
['a', 'b', 'c', 'd']
['\na', 'b', '\tc\nd\n']
['', 'a b \tc', 'd', '']
['\na ', ' \tc\nd\n']
['', 'a b \tc', 'd']
- partition(sep) -> (head, sep, tail)
从左至右,遇到分隔符就把字符串分割成两部分,返回头、分隔符、尾三部分的三元组
如果没有找到分隔符,就返回头、2个空元素的三元组
sep 分割字符串,必须指定
- rpartition(sep) -> (head, sep, tail)
从右至左,遇到分隔符就把字符串分割成两部分,返回头、分隔符、尾三部分的三元组
如果没有找到分隔符,就返回2个空元素和尾的三元组
s = ','.join('abcd')
print(s.partition(','))
print(s.partition('.'))
print(s.rpartition(','))
print(s.rpartition('.'))
('a', ',', 'b,c,d')
('a,b,c,d', '', '')
('a,b,c', ',', 'd')
('', '', 'a,b,c,d')
5 替换
- replace(old, new[, count]) -> str
字符串中找到匹配替换为新子串,返回新字符串
count表示替换几次,不指定就是全部替换
s = ','.join('abcd')
print(s.replace(',', ' '))
print(s.replace(',', ' ', 2))
s1 = 'www.google.edu'
print(s1.replace('w', 'a'))
print(s1.replace('ww', 'a'))
print(s1.replace('ww', 'w')) # 返回什么?
print(s1.replace('www', 'a'))
a b c d
a b c,d
aaa.google.edu
aw.google.edu
ww.google.edu
a.google.edu
6 移除
- strip([chars]) -> str
在字符串两端去除指定的字符集chars中的所有字符
如果chars没有指定,去除两端的空白字符 - lstrip([chars]) -> str ,从左开始
- rstrip([chars]) -> str,从右开始
s = '\t\r\na b c,d\ne\n\t'
print(s.strip())
print('-' * 30)
print(s.strip('\t\n'))
print('-' * 30)
print(s.strip('\t\ne\r'))
7 其他函数
- endwith
- startwith
- upper()大写
- lower()小写
- swapcase() 交换大小写
- isalnum() -> bool 是否是字母和数字组成
- isalpha() 是否是字母
- isdecimal() 是否只包含十进制数字
- isdigit() 是否全部数字(0~9)
- isidentifier() 是不是字母和下划线开头,其他都是字母、数字、下划线
- islower() 是否都是小写
- isupper() 是否全部大写
- isspace() 是否只包含空白字符