<>列表的添加

1. 列表的末尾添加一个元素:append()
例:
lst1=list(['hello','world',98]) print('添加元素之前:',lst1) lst1.append('python')
print('添加元素之后:',lst1)
结果:

添加元素之前: [‘hello’, ‘world’, 98]
添加元素之后: [‘hello’, ‘world’, 98, ‘python’]

2.在列表的末尾至少添加一个元素:extend()

例:
lst1=['hello','world',10,20] lst2=['python','YYDS'] lst1.append(lst2)
#将lst2作为一个元素添加到列表的末尾 print(lst1) lst1.extend(lst2) print(lst1)
结果:

[‘hello’, ‘world’, 10, 20, [‘python’, ‘YYDS’]]
[‘hello’, ‘world’, 10,20, [‘python’, ‘YYDS’], ‘python’, ‘YYDS’]

注意:其中lst1.append(lst2)将lst2作为一个元素添加到列表的末尾,lst2的中括号也在lst1中

3.在列表的任意位置添加一个元素:insert()
格式:

列表变量.insert(插入位置,添加数据)

例:
lst1=['hello','world',10,20] lst1.insert(1,90)
#在1的位置上添加90,原本在1的位置上的数字变为2,其余依次往后推 print(lst1)
结果:

[‘hello’, 90, ‘world’, 10, 20]

4.在列表的任意位置至少添加一个元素:切片
格式:

列表变量1[切入位置:切片距离]=列表变量2

例一:
```bash lst1=['hello','world',10,20,30,40,] lst=[True,False,'python'] lst1[1:]
=lst#第一个数字表示插入起始位置,第二个数字表示间距,没有写的话默认为将切片完后面的数据全部切掉 print(lst1)
结果:

[‘hello’, True, False, ‘python’]

#结果分析:lst1[1:]=lst,lst中一共有三个数据从第一个数据开始替换为True,一直到第三个数据换为’python’,由于第二个没有数字,因此’python’后面的数据全部切掉

例二:
lst1=['hello','world',10,20,30,40,] lst=[True,False,'python'] lst1[1:1]=lst
print(lst1)
结果;

[‘hello’, True, False, ‘python’, ‘world’, 10, 20, 30, 40]

例三:
lst1=['hello','world',10,20,30,40,] lst=[True,False,'python'] lst1[1:2]=lst
print(lst1)
结果:

[‘hello’, True, False, ‘python’, 10, 20, 30, 40]

#例二与例三只是少了个’world’,去掉多少个原来的元素,可以用后面的数字减去前面的例三中lst1[1:2],用2-1=1,就可以知道去掉一个原来的元素’world’

<>列表的删除

- remove():

* 一次删除一个元素
* 重复元素只删除第一个
* 元素不存在抛出ValueError
例:
lst=['hello','world',10,20,30,40,] lst.remove(10) print(lst)
结果:

[‘hello’, ‘world’, 20, 30, 40]

- pop():

* 删除一个指定索引位置上的元素
* 指定索引不存在抛出IndexError
* 不指定索引,删除列表中的最后一个元素
例: lst=['hello','world','hello',10,20,30,40] lst.pop(1) print(lst) lst.pop()
#不指定元素,就删除最后一个 print(lst)
结果:

[‘hello’, ‘hello’, 10, 20, 30, 40]
[‘hello’, ‘hello’, 10, 20, 30]

- 切片:一次至少删除一个元素(注意切片可以留下不想要的数据,也可以是去掉不想要的数据)

去掉不想要的元素格式:

列表名[起始位置:末尾位置]=[]

注意:删除元素的本质是用空列表代替原来的元素,删除的元素个数用末尾位置-起始位置
例一:
lst=['hello','world','hello',10,20,30,40] lst1=lst[1:3]#此时的操作时留下了[1,3)上面的元素
print('原来的列表:',lst) print('切片后的列表:',lst1)
结果:

原来的列表: [‘hello’, ‘world’, ‘hello’, 10, 20, 30, 40]
切片后的列表: [‘world’, ‘hello’]

例二:
lst=['hello','world','hello',10,20,30,40] print('原来的列表:',lst) lst[1:3]=[]
#删除元素的本质是用空列表代替原来的元素 print('切片后的列表:',lst)
结果:

原来的列表: [‘hello’, ‘world’, ‘hello’, 10, 20, 30, 40]
切片后的列表: [‘hello’,10, 20, 30, 40]

* clear():清空列表
格式:

列表名.clear()

例:
lst=['hello','world','hello',10,20,30,40] print('原来的列表:',lst) lst.clear() print
('切片后的列表:',lst)
结果:

原来的列表: [‘hello’, ‘world’, ‘hello’, 10, 20, 30, 40]
切片后的列表: []

* del:删除列表
格式:

del 列表名

例:
lst=['hello','world','hello',10,20,30,40] del lst print(lst)
结果;

NameError: name ‘lst’ is not defined

<>列表的修改

* 列表元素的修改操作
1.为指定索引的元素赋予一个新值
2.为指定的切片赋予一个新值
例:
lst=[10,20,30,40] lst[2]=100#一次修改一个值 print(lst) lst[1:3]=[200,300]
#将列表中的1位置和2位置替换为200,300 print(lst) lst[1:3]=[50,60,70,80] print(lst)
#将列表中的1位置和2位置替换为50,60,70,80
结果:

[10, 20, 100, 40]
[10, 200, 300, 40]
[10, 50, 60, 70, 80, 40]

<>列表的排序

一、sort()函数
格式:

列表名.sort()
列表名.sort(reserve=True\False)

调用sort()方法,列有中的所有元素默认按照从小到大的顺序进行排序,可以指定reverse=True,进行降序排序

例:
lst=[10,65,30,25,51] print('排序前的列表:',lst) lst.sort() print('排序后的列表为:',lst)
lst.sort(reverse=True)#reserver=True表示降序排序 print('reverse=True排序为:',lst)
lst.sort(reverse=False)#reserve=False表示升序排序 print('reverse=False排序为:',lst)
结果:

排序前的列表: [10, 65, 30, 25, 51]
排序后的列表为: [10, 25, 30, 51, 65]
reverse=True排序为: [65, 51, 30, 25, 10]
reverse=False排序为: [10, 25, 30,51, 65]

二、调用内置函数sorted(),可以指定reverse=True进行降序排序,原列表不发生改变
(注意:sort()是对原列表进行排序,sorted是产生新的列表,使用sorted后原列表不发生变化)
例:
lst=[10,65,30,25,51] print('原列表',lst) lst1=sorted(lst) print(lst)#此时lst列表并不发生变化
print(lst1) #指定关键字参数,实现列表元素的降序排序 lst2=sorted(lst,reverse=True) print(lst2)
结果:

原列表 [10, 65, 30, 25, 51]
[10, 65, 30, 25, 51]
[10, 25, 30, 51, 65]
[65, 51, 30, 25, 10]

<>列表生成式

语法格式:

列表名=[i*i for i in range(1,10)]

#其中i*i中的乘号也可以换成其他的运算符

例:
lst=[i for i in range(1,10)] print(lst) lst=[i+i for i in range(1,10)] print(
lst)
结果:

[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 4, 6, 8, 10, 12, 14, 16, 18]

技术
今日推荐
PPT
阅读数 99
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信