2、序列类型及方法
课题:序列类型的方法
目的:掌握序列类型里面的方法对数据进行增删改查的操作
知识点一:列表的方法
列表 | 方法 | 描述 | |
---|---|---|---|
增 | append | 将元素追加到末尾 | Append object to the end of the list. |
insert | 添加到指定的位置 | Insert object before index. | |
extend | 将序列中的每个元素追加到末尾 | Extend list by appending elements from the iterable. | |
删 | pop | 不带参数弹出最后一个元素、带参数指定下标值 | Remove and return item at index (default last). |
remove | 移除指定的元素 | Remove first occurrence of value. | |
clear | 删除列表中的所有值 | Remove all items from list. | |
改 | li[0] = ' list ' | ||
查 | index | 在列表中从左至右查找指定元素,找到了放回该值的下标 | Return first index of value. |
count | 计算列表中指定元素出现的次数 | Return number of occurrences of value. | |
其他方法 | copy | 返回一个该列表的复制 | Return a shallow copy of the list. |
reverse | 对列表中的元素进行反向排列 | Reverse IN PLACE. | |
sort | 对列表中的元素进行从小到大的排列 | Stable sort IN PLACE. |
1 增(3种)
append,将元素追加到末尾
>>>li = [4,3,2,1]
>>>li.append(5)
[4,3,2,1,5]
insert,添加到指定的位置
>>>li.insert(索引位置,插入元素)
>>>li.insert(0,'hello')
>>>li
['hello',4,3,2,1,5]
extend,将序列中的每个元素追加到末尾
>>>a = [1,2,3]
>>>b = ['a','b','c']
>>>a.extend(b)
>>>a
[1,2,3,'a','b','c']
2 删(4种)
del:删除指定元素
>>>del li[0]
pop:不带参数弹出最后一个元素、带参数指定下标值
>>>li = [5,3,2]
>>>li.pop()
2
>>>li.pop(0)
5
remove,移除指定的元素(没有返回值)
>>>li = [5,4,3,2,1]
>>>li.remove(5)
>>>li
[4,3,2,1]
clear、删除列表中的所有值
>>>li.clear()
>>>li
[]
3 改(1种)
重新赋值
>>>li[0]='python'
4 查(2种)
`index,在列表中从左至右查找指定元素,找到了放回该值的下标`
>>>li.index(4)
0
>>>li.index(查询元素,起始位置,结束位置)
count、计算列表中指定元素出现的次数
>>>li=[1,2,3,3,4]
>>>li.count(3)
2
5 其他方法
copy:复制一个新的列表
>>>a = li.copy()
>>>a
reverse、对列表中的元素进行反向排列
>>>li = [1,2,3]
>>>li
[1,2,3]
>>>li.reverse()# 不会产生新的列表
>>>li
[3,2,1]
sort、对列表中的元素进行从小到大的排列(同类型)
ASCII码
>>>li=[3,5,1]
>>>li.sort()
>>>li
[1,3,5]
dir 查看所有方法 help 查看帮助文档
拓展
A(拓展)Python列表脚本操作符
列表对 + 和 * 的操作符与字符串相似。+ 号用于组合列表,* 号用于重复列表。
如下所示:
Python 表达式 | 结果 | 描述 |
---|---|---|
len([1, 2, 3]) | 3 | 长度 |
[1, 2, 3] + [4, 5, 6] | [1, 2, 3, 4, 5, 6] | 组合 |
['Hi!'] * 4 | ['Hi!', 'Hi!', 'Hi!', 'Hi!'] | 重复 |
3 in [1, 2, 3] | True | 元素是否存在于列表中 |
for x in [1, 2, 3]: print x, | 1 2 3 | 迭代 |
B 函数
Python包含以下函数:
序号 | 函数 |
---|---|
1 | cmp(list1, list2) 比较两个列表的元素 |
2 | len(list) 列表元素个数 |
3 | max(list) 返回列表元素最大值 |
4 | min(list) 返回列表元素最小值 |
5 | list(seq) 将元组转换为列表 |
总结:必须掌握
append
insert
extend
pop
remove
知识点二:元祖
创建空元组
tup1 = ()
元组中只包含一个元素时,需要在元素后面添加逗号
tup1 = (50,)
元组与字符串类似,下标索引从0开始,可以进行截取,组合等。
tuple | 方法 | 描述 | |
---|---|---|---|
查 | count | 查数量 | Return number of occurrences of value. |
index | 查第一个元素 | Return first index of value. |
1 查
count()
index()
2 连接组合
>>>a=(1,2,3)
>>>b=(3,4,5)
>>>a+b
(1,2,3,4,5)
3 删
del:删除整个元祖
>>>del a
>>>a
name 'a' not defined
拓展
A 元组运算符
与字符串一样,元组之间可以使用 + 号和 * 号进行运算。这就意味着他们可以组合和复制,运算后会生成一个新的元组。
Python 表达式 | 结果 | 描述 |
---|---|---|
len((1, 2, 3)) | 3 | 计算元素个数 |
(1, 2, 3) + (4, 5, 6) | (1, 2, 3, 4, 5, 6) | 连接 |
('Hi!',) * 4 | ('Hi!', 'Hi!', 'Hi!', 'Hi!') | 复制 |
3 in (1, 2, 3) | True | 元素是否存在 |
for x in (1, 2, 3): print x, | 1 2 3 | 迭代 |
B 元组内置函数
Python元组包含了以下内置函数
序号 | 方法及描述 |
---|---|
1 | cmp(tuple1, tuple2) 比较两个元组元素。 |
2 | len(tuple) 计算元组元素个数。 |
3 | max(tuple) 返回元组中元素最大值。 |
4 | min(tuple) 返回元组中元素最小值。 |
5 | tuple(seq) 将列表转换为元组。 |
知识点三:字符串
字符串是不可变的
思考?【增删改】(--不是原来的了)查
a = "hello python"
str | 描述 | ||
---|---|---|---|
查 | count | 计算出现了多少次 | Return the number of non-overlapping occurrences of substring sub in string S[start:end]. |
find | 查找一端数据 | Return the lowest index in S where substring sub is found,such that sub is contained within S[start:end]. | |
index | 返回指定的下标 | Return the lowest index in S where substring sub is found,such that sub is contained within S[start:end]. | |
isdigit | 判断一个字符串里的数据是不是都是数字 | Return True if the string is a digit string, False otherwise. | |
isalpha | 判断每个元素是不是字母 | Return True if the string is an alphabetic string, False otherwise. | |
endswith | 比较结尾的元素是否相同 | Return True if S ends with the specified suffix, False otherwise. | |
startswith | 比较开头的元素是否相同 | Return True if S starts with the specified prefix, False otherwise. | |
islower | 判断字符串中的值是否全部是小写 | Return True if the string is a lowercase string, False otherwise. | |
isupper | 判断字符串中的值是否全部是大写 | Return True if the string is an uppercase string, False otherwise. | |
删 | replace | Return a copy with all occurrences of substring old replaced by new. | |
改 | upper | 把所有的字母转换成大写 | Return a copy of the string converted to uppercase. |
lower | 把所有字母转换成小写 | Return a copy of the string converted to lowercase. | |
strip | (lstrip,rstrip) 除去空格带参数去除指定元素的开头和结尾的相同的元素 | ||
capitalize | Return a capitalized version of the string. | ||
title | Return a version of the string where each word is titlecased. | ||
split | Return a list of the words in the string, using sep as the delimiter string. | ||
增 | join | Concatenate any number of strings. | |
字符串拼接 |
1 查
count、计算出现了多少次
a.count('o')
find、 如果包含子字符串返回开始的索引值,否则返回-1。
>>>a.find('hello')
0
>>>a.find('a')
-1
index、返回指定值的下标,没有会报错
isdigit、判断一个字符串里的数据是不是都是数字
isalpha、判断每个元素是不是字母
endswith、比较结尾的元素是否相同
startswith、比较结尾的元素是否相同
islower、判断字符串中的值是否全是小写的
isupper、判断字符串中的值是否全是大写的
2 改
upper、把所有的字母转成大写
lower、把所有的字母转成大写
strip (lstrip,rstrip)、去除空格、带参数去除指定元素的开头和结尾的相同的元素
>>>str = "00000003210Runoob01230000000";
>>>str.strip( '0' ); # 去除首尾字符 0
>>>a = ' hello python '
>>> a.strip('nmp')
'ytho'
>>>str2 = " Runoob "; # 去除首尾空格
>>>str2.strip();
编译器会去除两端所有相应的字符,直到没有匹配的字符。
>>>dodo = "say hello say boy saaayaaas"
>>>print(dodo.strip('say') )
hello say boy
capitalize、把第一个字母转换成大写
>>>a= "hello world"
Hello world
title、每个单词的首字母进行大写转换
split、没有参数默认以空格来切割、有参数
>>>a.split()# 默认为切割 特别常用 爬虫
>>>b = 'hello world python'
>>>l.split('o')
['hell',' ','w',]
3 删
replace、替换把对象中有的值放第一个参数中替换成第二个参数
str.replace(old, new[, max])
old -- 将被替换的子字符串。
new -- 新字符串,用于替换old子字符串。
max -- 可选字符串, 替换不超过 max 次(默认是全部,可以指定次数)
4 增
字符串拼接
5 转义
str的转义 | |
---|---|
\n | 换行 |
\t | 水平制表符 |
\b | 退格 |
\r | 回车,当前位置移到本行开头 |
\\ | 代表反斜杠 \ |
' | 代表一个单引号,同样的 “ 等符号也可以这么输出 |
\0 | 代表一个空字符 |
\a | 系统提示音 |
交互式模式:是什么就显示什么
print:尽量展示为人类可以看懂得数据(调用__ str __)
字符前面加上 \ ,字符就不再表示字符本身的意思,表示ASCII码中不能显示字符,常见有下:
\n 换行
\t 水平制表符 Tab 四个空格
\b 退格
\r 回车,当前位置移到本行开头
\\ 代表反斜杠 \
\’ 代表一个单引号,同样的 “ ? 等符号也可以这么输出
\0 代表一个空字符
\a 系统提示音
在python中如果要去掉字符串的转义,只需要在字符串前面加上 r
r'abc\tabc'
>>>a = "abc\babc"
>>>print(a)
"ababc"
>>>print(r"abc\babc")
"abc\babc"
字符串方法本身不会变,但是我们可以拿来赋值
拿来用就很简单了
总结:必须掌握:
count
find
isdigit
replace
split
\n
r
熟悉使用 replace split
知识点四:字符串拼接
+
'abc'+'def'
%s
>>>"%s + %s"%('hello','python')
join
>>>"-".join("hello")
>>>"-".join(['hello','world'])
format
'{} {} {}'.format('I','love','you')
'{0} {1} {2}'.format('a','b','c')
'{n1} {n2} {n3}'.format(n1='name',n2='age',n3='sex')
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App