<>序列

<>索引

元素1元素2元素3元素4元素5元素6
012345
-6-5-4-3-2-1 verse = ["LSF","LYF","LJZ","ZSF","YX"] print(verse[0]) print(verse
[-1]) print(verse[1])
LSF YX LYF

<>切片

sname[strart :end;step]
end不包括本事
strp步长默认为1,可以省略
verse = ["LSF","LYF","LJZ","ZSF","YX"] print(verse[0:4:3])
[‘LSF’, ‘ZSF’]
注意:复制整个系列用
print(verse[:])即可

<>序列相加

不同元素类型也可以相加,但是序列和元组和字符串不能相加
verse = ["LSF","LYF","LJZ","ZSF","YX"] football = ["axle","cl","mx"] luckynum =
[3,10,33] print(verse+football) print(football+luckynum)
结果:[‘LSF’, ‘LYF’, ‘LJZ’, ‘ZSF’, ‘YX’, ‘axle’, ‘cl’, ‘mx’]
[‘axle’, ‘cl’, ‘mx’, 3, 10, 33]

<>乘法
family = ["LSF","LYF","LJZ","ZSF","YX"] football = ["axle","cl","mx"] luckynum
= [3,10,33] print(family*2)
[‘LSF’, ‘LYF’, ‘LJZ’, ‘ZSF’, ‘YX’, ‘LSF’, ‘LYF’, ‘LJZ’, ‘ZSF’, ‘YX’]
可以用乘法生成指定长度列表

<>查询元素是否在序列中

value in sequence
value 表示元素,sequence表示序列
family =["lsf","zyy","yx","lyf","zsf","ljz",6,8,33] print("zyy" in family)
print(33 in family)
Ture Ture
注意:字符串要用引号

<>计算序列的长度、最大值和最小值
family =["lsf","zyy","yx","lyf","zsf","ljz",6,8,33] print("序列family的长度是:",len(
family)) print("序列family的长度是:"+str(len(family)))
序列family的长度是: 9
序列family的长度是:9
family =[66,6,8,33,23,44,67] print("序列",family,"中的最大值是:",max(family),"/""序列",
family,"的最小值是",min(family))
序列 [66, 6, 8, 33, 23, 44, 67] 中的最大值是: 67 /序列 [66, 6, 8, 33, 23, 44, 67] 的最小值是 6
python提供的内置函数

函数说明
len()求序列长度
max()求最大值
min()求最小值
list()将序列转化成列表
str()将序列转换成字符串
sum()计算元素和
sorted()对元素进行排序
reversed反向序列中的元素
enumerate()将序列组合成一个索引序列,用于for循环 family =[66,6,8,33,23,44,67] u=str(family)
print(type(u)) print(sorted(family)) print(list(reversed(family))) //reversed
返回的是一个迭代器,用list
<class ‘str’>
[6, 8, 23, 33, 44, 66, 67]
[67, 44, 23, 33, 8, 6, 66]

<>列表

<>列表创建和删除

1、直接创建
listname = [element1,element2,element3,…]
2、创建空列表
emptylist= []
3、数值列表
list(data)
data可以是range对象,字符串,元组,其他可迭代类型的数据
ab = list(range(1,10,3)) print(ab)
[1, 4, 7]
4、删除列表
del listname

<>访问列表元素
family =["lsf","zyy","yx","lyf","zsf","ljz",6,8,33] print(family) print(family[
1:10:2])
[‘lsf’, ‘zyy’, ‘yx’, ‘lyf’, ‘zsf’, ‘ljz’, 6, 8, 33]
[‘zyy’, ‘lyf’, ‘ljz’, 8]

<>遍历列表

1、用for循环实现
for name in listname
print(name)
family =["lsf","zyy","yx","lyf","zsf","ljz",6,8,33] for familyname in family:
print(familyname)
lsf
zyy
yx
lyf
zsf
ljz
6
8
33
name = "python" for name1 in name: print(name1)
p
y
t
h
o
n

2、使用for和enumerate
for index,name in enumerate(listname)
print(index,name)
for index,pyl in enumerate("python"): print(index,pyl)
0 p
1 y
2 t
3 h
4 o
5 n
family =["lsf","zyy","yx","lyf","zsf","ljz",6,8,33] for index , familyname in
enumerate(family): print(index+1,familyname)
1 lsf
2 zyy
3 yx
4 lyf
5 zsf
6 ljz
7 6
8 8
9 33

<>添加、修改、删除列表元素

1、添加元素

listname.append(obj) //添加一个元素
family =["lsf","zyy","yx","lyf","zsf","ljz"] family.append("lef") print(family)
[‘lsf’, ‘zyy’, ‘yx’, ‘lyf’, ‘zsf’, ‘ljz’, ‘lef’]

listname.extend(seq) //添加列表
family =["lsf","yx","lyf","zsf","ljz"] fk =["zyy","zyq","zfr","lxb"] family.
extend(fk) print(family)
[‘lsf’, ‘yx’, ‘lyf’, ‘zsf’, ‘ljz’, ‘zyy’, ‘zyq’, ‘zfr’, ‘lxb’]

2、修改元素
修改元素只需要通过索引获取元素后,重新给他赋值就可以了
family =["lsf","yx","lyf","zsf","ljz"] print(family) family[2] ="lyf and lef"
print(family)
[‘lsf’, ‘yx’, ‘lyf’, ‘zsf’, ‘ljz’]
[‘lsf’, ‘yx’, ‘lyf and lef’, ‘zsf’, ‘ljz’]

3、删除元素
family =["lsf","yx","lyf","zsf","ljz"] print(family) del family[1] print(family
)
[‘lsf’, ‘yx’, ‘lyf’, ‘zsf’, ‘ljz’]
[‘lsf’, ‘lyf’, ‘zsf’, ‘ljz’]

删除不确定位置的函数用remove
family =["lsf","yx","lyf","zsf","ljz"] print(family) family.remove("yx"): print
(family)
[‘lsf’, ‘yx’, ‘lyf’, ‘zsf’, ‘ljz’]
[‘lsf’, ‘lyf’, ‘zsf’, ‘ljz’]
思考:如果是有2个yx怎么处理?

首先判断元素是否在里面
family =["lsf","yx","lyf","zsf","ljz"] print(family) LZ= "lsf" in family if LZ
== True: family.remove("yx") print(family)
[‘lsf’, ‘yx’, ‘lyf’, ‘zsf’, ‘ljz’]
[‘lsf’, ‘lyf’, ‘zsf’, ‘ljz’]

<>列表中数据统计

1、获取指定元素出现的次数
listname.count(obj)
family =["lsf","yx","lyf","zsf","ljz","yx"] num = family.count("yx") print(num)
2

2、获取指定元素首次出现的下标
listname.index(obj)
family =["lsf","yx","lyf","zsf","ljz","yx"] index1 = family.index("yx") print(
index1)
1
3、统计数值列表的元素和
sum(iterable[,start])
num1 = [1,2,3,4,5,6,7,8,9] sumnum = sum(num1[1:5]) print(sumnum)
14

<>对列表进行排序

1、使用列表对象sort()方法实现
listname.sort(key=None,reverse=False
num1 = [1,7,5,15,66,2] num1.sort() print(num1) num1.sort(reverse=True) print(
num1)
[1, 2, 5, 7, 15, 66]
[66, 15, 7, 5, 2, 1]
char = ["zyy","zyq","yx","lxb","Zfr"] char.sort() print(char) char.sort(key=str
.lower) print(char)
[‘Zfr’, ‘lxb’, ‘yx’, ‘zyq’, ‘zyy’]
[‘lxb’, ‘yx’, ‘Zfr’, ‘zyq’, ‘zyy’]

2、使用内置的sorted()函数实现
num1 = [1,7,5,15,66,2] num =sorted(num1,reverse=True) num2= sorted(num1) print(
num) print(num2)
[66, 15, 7, 5, 2, 1]
[1, 2, 5, 7, 15, 66]

sort()和sorted()功能都是一样的,但是sorted要建立一个新的副本,

<>列表推导式

用于快速生成列表
1、指定范围
list = [Expression for var in range]
import random rdnum =[random.randint(1,100) for i in range(5)] print(rdnum)
2、根据列表生成指定需求列表
newlist = [Expression for var in list]
price = [100,40,35,27,40] sale =[x*0.5 for x in price] print((sale))
[50.0, 20.0, 17.5, 13.5, 20.0]
3、旧列表中选择符合条件的元素添加到新列表
newlist = [Expression for var in list if condition]
price = [100,50,70,60,30] sale = [x for x in price if x>50] print(sale)
[100, 70, 60]

<>元组

1、创建元组
tuplename = (element1, element2,element3,…)
用小括号,也可以不用,只要有逗号隔开即可
family1 =("lsf") family2 =("lsf",) print(type(family1))
print("类型是:",type(family2))
<class ‘str’>
类型是: <class ‘tuple’>
注意逗号

2、创建空元组
emptytuple =()

3、创建数组
tuplename = tuple(range(10,20,3)) print(tuplename)
(10, 13, 16, 19)

4、删除元组
del tuplaname

5、访问元组
print(tuplename)
print(tuplename[2])

<>列表和元组的区别

1列表属于可变序列,元组不能
2列表可以使用append()、extend() instrat() remove()和pop()等方法,元组不能
3 列表可以切片和修改,元组可以切片
4 元组比列表访问更快,
5 列表不能作为字典的建

1、实战任务
year = [89,98,00,75,68,37,58,90] # 原有的年份列表 for index,value in enumerate(year):
# 遍历列表元素索引与年份 if str(value)!='0': # 判断非0年份 year[index]=int('19'+str(value))
else: year[index]=int('200'+str(value)) # 判断2000年 print(year) # 打印修改后的年份列表
[1989, 1998, 2000, 1975, 1968, 1937, 1958, 1990]

技术
友情链接
码工具
Toolsou
API参考文档
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信