Python学习第四天

列表

列表是python中的最基本的数据结构之一,列表中的每个元素都分配给他一个索引,索引是从0开始,然后依次递增,0,1,2,3,4…
列表的特征就是以 “ [] ” 将所有元素绑在一起,并用 “ , ” 将每个元素分隔开的序列。

创建列表

创建一个普通的列表

x = [ 1,2,3,4,5]
print(x,type(x))
#[1, 2, 3, 4, 5] <class 'list'>

利用range()创建列表

x = list(range(1,100,10))
print(x,type(x))
#[1, 11, 21, 31, 41, 51, 61, 71, 81, 91] <class 'list'>

利用推导式创建列表

x = [i for i in range(1,100,10)]
print(x,type(x))
#[1, 11, 21, 31, 41, 51, 61, 71, 81, 91] <class 'list'>

创建一个空列表

x = []
print(x,type(x))
#[] <class 'list'>

列表的增操作

  • append()添加是将括号中的内容(可以是一个值,一个字符串,或者一个列表),当作一个值插在列表的最后,例:
x = [1,"hello",2,'world']
x.append([3,'hello world',4])
print(x,type(x))
#[1, 'hello', 2, 'world', [3, 'hello world', 4]] <class 'list'>
  • extend()添加类似于扩展,将其中的值依次插入列表中成为一个新的值,例:
x = [1,"hello",2,'world']
x.extend([3,'hello world',4])
print(x,type(x))
#[1, 'hello', 2, 'world', 3, 'hello world', 4] <class 'list'>
  • insert()添加可以在需要的某一个索引位置添加所需要插入的值,例:
x = [1,"hello",2,'world']
x.insert(3,'hello world')   #从0开始数,数到3时插入值
print(x,type(x))
#[1, 'hello', 2, 'hello world', 'world'] <class 'list'>

列表的删操作

  • remove()操作是将列表中匹配到的第一个值删除,例:
x = [1,"hello",2,'world']
x.remove(2)    #匹配列表中为”2“的值,匹配到则删除第一个
print(x,type(x))
#[1, 'hello', 'world'] <class 'list'>
  • pop()操作是删除列表中指定索引的值,如若未指定,则删除最后一个值,并返回这个值,例:
x = [1,"hello",2,'world']
y = x.pop()
print(y,type(y))
print(x,type(x))
# world <class 'str'>
# [1, 'hello', 2] <class 'list'>
  • del var1[, var2 ……]操作可以删除多个值,例:
x = [1,"hello",2,'world']
del x[2:4]
print(x,type(x))
# [1, 'hello'] <class 'list'>

列表元素统计

count()可以统计列表中元素出现的次数,例:

x = [1,"hello",2,'world',1,"hello",2,'world',1,"hello",2,'world']
y = x.count('world')   #统计world出现的次数
print(y)
# 3

列表的反向操作

reverse()可以将列表中的元素反向排序,例

x = [1,"hello",2]
x.reverse()
print(x)
#  [2, 'hello', 1]

列表的排序操作

sort()可以对列表进行排序,reverse = True 降序, reverse = False 升序,如若未设置,则默认未升序,例

x = [1,3,2,41,53,2,4]
x.sort(reverse=True)
print(x)
# [53, 41, 4, 3, 2, 2, 1]

元组

元组和列表类似,不同于列表的是创建后不能再进行修改,同时是使用 ”()" 来将元素绑在一起(不用括号亦可,一般建议添加)。

创建元组

x = (1,)  #如若元组中只有一个元素,后面需要添加”,”,否则()会视为运算符
y = (1,"hello",2)
z = ("hello",)*3
i = (1,2,3),("hello","world")
print(x,type(x))
print(y,type(y))
print(z,type(z))
print(i,type(i))
# (1,) <class 'tuple'>
# (1, 'hello', 2) <class 'tuple'>
# ('hello', 'hello', 'hello') <class 'tuple'>
# ((1, 2, 3), ('hello', 'world')) <class 'tuple'>

元组的改操作

前面说到,元组和列表不同,一旦创建便不可修改,但是,如若元组的元素是可修改的类型(例如列表),则可以对元素列表进行修改,例:

x = (1,2,3,4,[5,6,7,8])
x[1]=9   #这里对不可修改的类型进行修改,查看结果
print(x,type(x))

运行结果为:

TypeError                                 Traceback (most recent call last)
<ipython-input-21-a231a3767a57> in <module>
      1 x = (1,2,3,4,[5,6,7,8])
----> 2 x[1]=9
      3 print(x,type(x))
      4 x[4][0] = 9
      5 print(x,type(x))

TypeError: 'tuple' object does not support item assignment

很明显,元组类型是不可以修改的,正确的操作为:

x = (1,2,3,4,[5,6,7,8])
x[4][0] = 9
print(x,type(x))
# (1, 2, 3, 4, [9, 6, 7, 8]) <class 'tuple'>

元组元素统计

count()可以统计元组中元素出现的次数,例:

x = (1,2,3,4,3,2,3,4,3)
y = x.count(3) #统计元素3出现的次数
print(y)
# 4

元组索引位置

index()可以统计元组元素的索引位置,例:

x = (1,2,3,4,3,2,3,4,3)
y = x.index(3)     #统计元素3第一次出现的索引位置
z = x.index(3,4,6) #统计元素3从索引4位置到索引6位置第一次出现的索引位置
print(y)
print(z)
# 2
# 4

学习地址为阿里天池的阿里天池龙珠计划python训练营,地址:https://tianchi.aliyun.com/s/bcfaedf7a7961e48effcc495bd5ee9d0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值