序列类型的方法

一、序列类型

序列指一组有序的元素

1、列表 [list] :一种可变序列,用【】表示,元素可被修改、添加、删除,查询

2、元祖(tupie):一种不可变序列,用()表示,元素不可修改,可索引和切片

3、字符串“str”:一种不可变序列,用单双引号表示,元素可修改,删除替换, 索引和切片

二、方法

1.1、列表【list】 —— 添加

append:在列表的末尾添加一个元素—— 示例:列表名.append(‘元素’

lst = [1, 2, 3, 4, 5,]
lst.append(0)
# append方法一次只能添加一个
lst.append('你好')
print(lst)



F:\python\python.exe "D:\muyi\my python\练习.py" 
[1, 2, 3, 4, 5, 0, '你好']

Process finished with exit code 0

insert:在列表指定位置插入一个元素 —— 示例:列表名.insert(需要插入的索引,内容)

lst = [1, 2, 3, 4, 5,]
# 在第 3 个位置插入一个元素(你好)3 为索引 从 0 开始数
lst.insert(3,'你好')
print(lst)






F:\python\python.exe "D:\muyi\my python\练习.py" 
[1, 2, 3, '你好', 4, 5]

Process finished with exit code 0

extend:在列表末尾添加另一个列表的所有元素 —— 示例:列表名.extend(另一个列表名)

lst = [1, 2, 3, 4, 5,]
list = [6,7,8,9,0]
# extend 在列表末尾添加另一个列表的所有元素
lst.extend(list)
print(lst)





F:\python\python.exe "D:\muyi\my python\练习.py" 
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

Process finished with exit code 0

1.2、列表【list】—— 删除

pop:移除列表中的一个元素,默认最后一个,并返回该元素的值—— 示例:列表名.pop(索引)

lst = [1, 2, '你好',3, 4, 5,]
lst2 = [123,321,1,2,3]
# pop 删除列表中指定元素,默认最后一个
lst.pop(2)  # 索引以 0 开始数,
# 默认最后一个
lst2.pop()
print(lst)
print(lst2)




F:\python\python.exe "D:\muyi\my python\练习.py" 
[1, 2, 3, 4, 5]
[123, 321, 1, 2]

Process finished with exit code 0

remove:移除列表中指定元素—— 示例:列表名.remove(要删除的元素)

lst = [1, 2, '你好',3, 4, 5,]
lst2 = [123,321,1,2,3]
# remove 删除列表中指定元素,
lst.remove(3)
# 制定元素,和pop的区别是  pop用索引指定删除,remove则直接指定元素删除
lst2.remove(321)
print(lst)
print(lst2)




F:\python\python.exe "D:\muyi\my python\练习.py" 
[1, 2, '你好', 4, 5]
[123, 1, 2, 3]

Process finished with exit code 0

clear:删除列表中所有的元素—— 示例:列表名.clear

lst = [1, 2, '你好',3, 4, 5,]
# clear 删除列表中所有元素,
lst.clear()
print(lst)




F:\python\python.exe "D:\muyi\my python\练习.py" 
[]

Process finished with exit code 0

1.3\列表【list】—— 修改

直接利用索引修改列表中指定位置的元素—— 示例:变量名【索引】=修改的内容

lst = [1, 2, '你好',3, 4, 5,]
# lst[索引]直接修改列表中制定位置的元素,
lst[5]='haluo'
print(lst)





F:\python\python.exe "D:\muyi\my python\练习.py" 
[1, 2, '你好', 3, 4, 'haluo']

Process finished with exit code 0

1.4、列表【list】——查询

index:指定元素在列表中第一次出现的位置(索引)——新变量名=列表名.index(列表中的内容)

lst = [1, '哈罗', '你好',3, '木易', 5,]
# index 指定元素在列表中第一次出现的位置(索引)
a =lst.index('木易')
print(a)




F:\python\python.exe "D:\muyi\my python\练习.py" 
4

Process finished with exit code 0

count:制定元素在列表中出现的次数——变量名=列表名.count(列表中的元素)

lst = [1, '木易', '你好', 3, '木易', 5, '木易']
# count 指定元素在列表中出现的次数
a = lst.count('木易')
print(a)




F:\python\python.exe "D:\muyi\my python\练习.py" 
3

Process finished with exit code 0

1.5、列表【list】——其他方法

copy:复制列表中的元素——示例:新变量=列表名.copy()

lst = [1, '木易', '你好', 3, '木易', 5, '木易']
# copy 复制列表中元素
a = lst.copy()
print(a)




F:\python\python.exe "D:\muyi\my python\练习.py" 
[1, '木易', '你好', 3, '木易', 5, '木易']

Process finished with exit code 0

reverse:反转裂变中的元素—— 示例:列表名.reverse()

lst = [1,2,3,4,'木易',5]
# reverse 反转列表中的元素
lst.reverse()
print(lst)



F:\python\python.exe "D:\muyi\my python\练习.py" 
[5, '木易', 4, 3, 2, 1]

Process finished with exit code 0

sort:给列表中的元素排序——升序,示例:列表名.sort()

lst = [1,2,35,3,40,4,10,5]
# sort 给列表中元素排序——升序
lst.sort()
print(lst)



F:\python\python.exe "D:\muyi\my python\练习.py" 
[1, 2, 3, 4, 5, 10, 35, 40]

Process finished with exit code 0

sort:给列表中的元素排序——降序,示例:列表名.sort(reverse=True)

lst = [1,2,35,3,40,4,10,5]
# sort 给列表中元素排序——降序
lst.sort(reverse=True)
print(lst)



F:\python\python.exe "D:\muyi\my python\练习.py" 
[40, 35, 10, 5, 4, 3, 2, 1]

Process finished with exit code 0

2.1、元祖(tuple)—— 添加

+:将两个元祖合并成一个新的元祖——示例:元祖1+元祖2

tuple = ('木易','小红','letting go')
tuple2 = ('你好呀','哈啰')
# + 将两个元祖合并成一个元祖
a = tuple + tuple2
print(a)




F:\python\python.exe "D:\muyi\my python\练习.py" 
('木易', '小红', 'letting go', '你好呀', '哈啰')

Process finished with exit code 0

*:将元祖指定重复次数——示例:元祖名 *

tuple = ('木易','小红','letting go')
# + 将元祖指定重复次数
a = tuple * 4  # 重复4次
print(a)




F:\python\python.exe "D:\muyi\my python\练习.py" 
('木易', '小红', 'letting go', '木易', '小红', 'letting go', '木易', '小红', 'letting go', '木易', '小红', 'letting go')

Process finished with exit code 0

2.2、元祖(tuple)—— 查询

count:指定元素在元祖中出现的次数 —— 示例:元祖名.count(元素)

mytuple = ('木易','小红','letting go','木易')
# count  指定元素在元祖中出现的次数
a = mytuple.count('木易')  # 木易出现两次
print(a)




F:\python\python.exe "D:\muyi\my python\练习.py" 
2

Process finished with exit code 0

index:指定元素在元祖中第一次出现的索引 —— 示例:元祖名.index(元素)

mytuple = (123,'木易','小红','letting go','木易',123,456,123,)
# index  指定元素在元祖中第一次出现的索引
a = mytuple.index('木易')  # 木易 第一次出现的索引为 1
b = mytuple.index(123)   # 123第一次出现的索引为 0
print(a)
print(b)



F:\python\python.exe "D:\muyi\my python\练习.py" 
1
0

Process finished with exit code 0

len:返回元祖中元素的个数 —— len(元祖名)

mytuple = (123,'木易','小红','letting go','木易',123,456,123,)
# len 返回元组中元素个数
a = len(mytuple)
print(f'mytuple元组中有 {a} 个元素')






F:\python\python.exe "D:\muyi\my python\练习.py" 
mytuple元组中有 8 个元素

Process finished with exit code 0

2.3、元祖(tuple)—— 内置方法

max:返回元祖中元素最大值 —— 示例:max(元祖名)

mytuple = (123,987,456,123,)
# max 返回元祖中元素的最大值
a = max(mytuple) 
print(f'mytuple元组中元素的最大值是: {a} ')




F:\python\python.exe "D:\muyi\my python\练习.py" 
mytuple元组中元素最大值是: 987 

Process finished with exit code 0

min:返回元祖中元素的最小值 —— 示例:min(元祖名)

mytuple = (123,987,456,123,)
# min 返回元祖中元素的最小值
a = min(mytuple)
print(f'mytuple元组中元素的最小值是: {a} ')




F:\python\python.exe "D:\muyi\my python\练习.py" 
mytuple元组中元素的最小值是: 123 

Process finished with exit code 0

tuple:将列表或者其他可迭代对象转换为元组 —— 示例:tuple(元祖名)

mylist = [123,987,456,123,]
mystr = '你好,123'
# tuple 将列表或者其他可迭代对象转换为 元组
a = tuple(mylist)  # 列表转元组
b = tuple(mystr)  # 字符串转元祖
print(a)
print(b)




F:\python\python.exe "D:\muyi\my python\练习.py" 
(123, 987, 456, 123)
('你', '好', ',', '1', '2', '3')

Process finished with exit code 0
3.1、字符窜“str” —— 添加

+ :变量名+变量名

mystr = '哈啰'
mystr2 = '你好'
a = mystr + mystr2
print(a)





F:\python\python.exe "D:\muyi\my python\练习.py" 
哈啰你好

Process finished with exit code 0

%s:占位符

myname = '木易'
myage = '18'
a = '我叫:%s ,我今年:%s 岁'%(myname,myage)
print(a)



F:\python\python.exe "D:\muyi\my python\练习.py" 
我叫:木易 ,我今年:18 岁

Process finished with exit code 0

join:分隔符.join(变量名)

mystr = '2023','12','02'
a = '-'.join(mystr)
print(a)



F:\python\python.exe "D:\muyi\my python\练习.py" 
2023-12-02

Process finished with exit code 0

f‘插值法’

name = '木易'
age = 25
print(f'我叫:{name},今年{age}岁')




F:\python\python.exe "D:\muyi\my python\练习.py" 
我叫:木易,今年25岁

Process finished with exit code 0
3.2、字符窜“str” —— 删除

replace:替换,对象中第一个替换成第二个—— 示例:新变量 = 变量名.replace(‘被替换值,‘替换值’’)

name = '木易,小红'
# replace 指定元素替换
a = name.replace('小红','你好')  # 蒋 小红 替换成 你好
print(a)




F:\python\python.exe "D:\muyi\my python\练习.py" 
木易,你好

Process finished with exit code 0
3.3、字符窜“str” —— 修改

upper:将字符串中所有字母转为大写字母 —— 示例:变量名=字符串名.upper()

mystr = "abcdefghijklmnopqrstuvwxyz"
a = mystr.upper()
print(a)



F:\Python3.9\python.exe "D:\muyi\my python\练习.py" 
ABCDEFGHIJKLMNOPQRSTUVWXYZ

Process finished with exit code 0

lower:将字符串中所有字母转为小写 ——示例:变量名=字符串名.lower()

mystr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
a = mystr.lower()
print(a)




F:\Python3.9\python.exe "D:\muyi\my python\练习.py" 
abcdefghijklmnopqrstuvwxyz

Process finished with exit code 0

strip:去掉字符串首尾指定字符 —— 示例:变量名=字符串名.strip(‘首尾指定内容’)

mystr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
a = mystr.strip('Z')  # 只能删除首尾 
print(a)




F:\Python3.9\python.exe "D:\muyi\my python\练习.py" 
ABCDEFGHIJKLMNOPQRSTUVWXY

Process finished with exit code 0
3.4、字符窜“str” —— 查询

count:计算元素出现多少次 —— 示例:变量名.count(“元素”)

mystr = "12312311"
a = mystr.count('1')  # 1 出现 4 次
print(a)




F:\Python3.9\python.exe "D:\muyi\my python\练习.py" 
4

Process finished with exit code 0

find:如果包含子字符串返回开始的索引值,没有则返回 -1 —— 示例:变量名.find(‘元素’)

mystr = "12312311"
a = mystr.find('1')  # 1的索引值为 0  以第一个为准
b= mystr.find('4')  # 没有 4 这个元素 则返回 -1
print(a)
print(b)




F:\Python3.9\python.exe "D:\muyi\my python\练习.py" 
0
-1

Process finished with exit code 0

index:返回指定值的下标 ,没有会报错 —— 变量名.index('元素‘)

mystr = "12312311"
a = mystr.index('2')  # 2的索引值为 1  以第一个为准
b = mystr.index('4')  # 没有 4 这个元素 会报错
print(a)
print(b)



F:\Python3.9\python.exe "D:\muyi\my python\练习.py" 
1

Process finished with exit code 0

#  报错信息

F:\Python3.9\python.exe "D:\muyi\my python\练习.py" 
Traceback (most recent call last):
  File "D:\muyi\my python\练习.py", line 3, in <module>
    b = mystr.index('4')  # 没有 4 这个元素 会报错
ValueError: substring not found

Process finished with exit code 1
转义字符

【 \n 】换行

【 \t 】制表符 / 四个空格

【 \b 】退格 / 删除\b前的那一个字符

mystr = "123\b456"
print(mystr)




F:\Python3.9\python.exe "D:\muyi\my python\练习.py" 
12456

Process finished with exit code 0

【 \r 】 删除 \r 左边数据

mystr = "123\r12311"
print(mystr)



F:\Python3.9\python.exe "D:\muyi\my python\练习.py" 
12311

Process finished with exit code 0

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值