首页 > 编程语言 > 详细

Python基础语法 第3节课 (列表)

时间:2020-04-22 20:16:47      阅读:63      评论:0      收藏:0      [点我收藏+]

[ ]列表

一、列表可以存放的内容

1.列表可以存放哪些类型?

 列表是一个大仓库,百宝箱,所学过的所有类型,都可以放在列表里面。

my_list = [1,2,3,(2,polo),6,hello,[lemon,python]]
print(my_list)

结果:[1, 2, 3, (2, ‘polo‘), 6, ‘hello‘, [‘lemon‘, ‘python‘]]

2.列表的长度len()

my_list = [1,2,3,(2,polo),6,hello,[lemon,python]]
print(len(my_list)) #7

3.获取列表中的元素---索引(类似字符串)

列表索引得到的结果还是列表(该元素是什么数据类型,索引得到的结果就是什么类型,不会发生变化)

my_list = [1,2,3,(2,polo),6,hello,[lemon,python]]
print(my_list[-1][1]) # python

4.列表的切片,还是个列表

 

二、创建列表

空列表

number = []
print(number) #[]

普通列表:

number = [1,2,3]
print(number) 

混合列表:

list_1 = [1,2,3,(2,‘polo‘),6,‘hello‘,[‘lemon‘,‘python‘]]
print(list_1)  # [1, 2, 3, (2, ‘polo‘), 6, ‘hello‘, [‘lemon‘, ‘python‘]]

 

列表的常用操作:增删改

三、向列表中添加元素

1.append() :默认在列表末尾追加新的对象。即将参数作为一个元素增加到列表尾部。(append的参数只有一个,append一次只能添加一个元素)

list_1 = [1,2,3,(2,‘polo‘),6,‘hello‘,[‘lemon‘,‘python‘]]
list_1.append(9)
print(list_1)  # [1, 2, 3, (2, ‘polo‘), 6, ‘hello‘, [‘lemon‘, ‘python‘], 9]

 ?append()是对列表有作用,但是其返回的结果是None,这个是python的规则。(remove() 同理.)

my_list = [1,2,3,(2,polo),6,hello,[lemon,python]]
new_elem = my_list.append(888)
print(my_list) 
print(new_elem)

结果:
[1, 2, 3, (2, ‘polo‘), 6, ‘hello‘, [‘lemon‘, ‘python‘], 888]
None

 

2.extend():在列表的尾部一次性追加另一个序列中的多个值,即将参数作为一个列表去扩展原列表,列表的合并。

list_1 = [1,2,3,(2,‘polo‘),6,‘hello‘,[‘lemon‘,‘python‘]]
list_1.extend([2,4,5,6])
print(list_1)  #[1, 2, 3, (2, ‘polo‘), 6, ‘hello‘, [‘lemon‘, ‘python‘], 2, 4, 5, 6]

 

3.insert():有2个参数,第一个参数是待插入的位置,第二个参数是在该待插入位置,插入的值。

list_1 = [1,2,3,(2,‘polo‘),6,‘hello‘,[‘lemon‘,‘python‘]]
list_1.insert(3,‘hello world‘)
print(list_1)  #[1, 2, 3, ‘hello world‘, (2, ‘polo‘), 6, ‘hello‘, [‘lemon‘, ‘python‘]]

 

四、删除列表中的元素

1.删除指定的内容(不知道索引的位置,只知道列表中有这个具体的内容,一定是 有 才可以删除,列表中不存在,是无法操作的)

list_1 = [1,2,3,(2,polo),6,hello,[lemon,python]]
list_1.remove([lemon,python])
print(list_1)  # [1, 2, 3, (2, ‘polo‘), 6, ‘hello‘]

remove()只能移除一个元素,列表中有多个相同元素,默认是移除第一个

list_1 = [1,‘hello‘,2,‘hello‘,3,(2,‘polo‘),6,‘hello‘,[‘lemon‘,‘python‘]]
list_1.remove(‘hello‘)
print(list_1)  #[1, 2, ‘hello‘, 3, (2, ‘polo‘), 6, ‘hello‘, [‘lemon‘, ‘python‘]]

 

2.删除指定的索引位置pop(),返回的结果是删除的元素值

list_1 = [1,2,3,(2,polo),6,hello,[lemon,python]]
#pop()不带索引值,默认移除列表中最后一个元素
list_1.pop()
print(list_1) # [1, 2, 3, (2, ‘polo‘), 6, ‘hello‘]
list_1 = [1,2,3,(2,polo),6,hello,[lemon,python]]
#pop()中带索引值,删掉对应索引值的内容
list_1.pop(3)
print(list_1)# [1, 2, 3, 6, ‘hello‘, [‘lemon‘, ‘python‘]]

?查看pop()的结果(返回值)

list_1 = [1,2,3,(2,polo),6,hello,[lemon,python]]
elem = list_1.pop()
print(elem)
print(list_1)

结果:

[‘lemon‘, ‘python‘]

[1, 2, 3, (2, ‘polo‘), 6, ‘hello‘]

 

五、修改列表中的元素

list_1 = [1,2,3,(2,polo),6,hello,[lemon,python]]
list_1[2] = hahhahahha
print(list_1)  #[1, 2, ‘hahhahahha‘, (2, ‘polo‘), 6, ‘hello‘, [‘lemon‘, ‘python‘]]

 

六、列表的函数方法

1.clear()  清楚列表元素

?a = my_list.clear()的返回值是None

my_list = [2,1,3]
my_list.clear()
print(my_list)      # []
print(my_list.clear())   #None

  

2.sort()  排序  数字排序,文字不需要排序

my_list = [2,1,3,4,6,5]
my_list.sort()
print(my_list)      # [1,2,3,4,5,6]
print(my_list.sort())   #None
my_list = [1,2,3,[3,6]]
my_list.sort()    #报错,不同类型是不能比较的

 

3.reverse()降序

my_list = [2,1,3,4,6,5]
my_list.sort(reverse=True)
print(my_list)      # [6,5,4,3,2,1]
print(my_list.sort(reverse = True))  #None

 

Python基础语法 第3节课 (列表)

原文:https://www.cnblogs.com/ananmy/p/12732567.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!