列表元素增删改排操作

一、列表元素的增加操作

  1. append():在列表的末尾添加一个元素
  2. extend():在列表的末尾至少添加一个元素
  3. insert():在列表的任意位置添加一个元素
  4. 切片:在列表的任意位置添加至少一个元素
#append():向列表的末尾添加一个元素
lst=[10,20,30]
print('添加元素前:',lst,id(lst))

lst.append(100)
print('添加元素后:',lst,id(lst))

lst.append('100')
print('添加元素后:',lst,id(lst))

lst2=['hello','world']
lst.append(lst2)
print('添加元素后:',lst,id(lst))    #[10, 20, 30, 100, '100', ['hello', 'world']] 1943091429504   lst2以一个元素的形式添加到lst中

#extend():向列表的末尾添加多个元素
lst.extend(lst2)
print(lst)  #[10, 20, 30, 100, '100', ['hello', 'world'], 'hello', 'world']  lst中添加多个元素

#insert():在任意位置添加一个元素
lst.insert(1,90)
print(lst)  #[10, 90, 20, 30, 100, '100', ['hello', 'world'], 'hello', 'world'] 在序号为1的位置添加元素90

#切片操作:在任意的位置上添加多个元素
lst3=['小秦同学在上学','喜欢和牛奶']
lst[1::]=lst3  #lst切片后面的数据全部被替代  [10, '小秦同学在上学', '喜欢和牛奶']
print(lst)

二、列表元素的删除操作

1、remove()

  • 一次删除一个元素
  • 重复元素只删除第一个
  • 元素不存在抛出ValueError
lst=[10,20,30,40,50,60,30]
print(lst)
lst.remove(30)    #从列表中删除一个元素,如果有重复元素则只移动第一个元素
print(lst)
#st.remove(100)   #ValueError: list.remove(x): x not in list 列表中不存在该元素

运行结果:

[10, 20, 30, 40, 50, 60, 30]
[10, 20, 40, 50, 60, 30]

2、pop()

  • 删除一个指定索引位置上的元素
  • 指定索引不存在抛出IndexError
  • 不指定索引,删除列表中最后一个元素
print(lst)
lst.pop()    #pop()如果不指定索引就删除列表最后的一个元素
print(lst)
lst.pop(0)   #删除索引号为0的元素,也就是第一个元素
print(lst)
#lst.pop(100) #IndexError: pop index out of range 越界,pop报错

运算结果:

[10, 20, 40, 50, 60, 30]
[10, 20, 40, 50, 60]
[20, 40, 50, 60]

3、切片:一次至少删除一个元素

print("切片删除操作——产生一个新的对象")
print(lst)
new_lst=lst[0::2]
print(new_lst)     #即使用新切片产生后的列表代替原列表使用

print("切片删除操作——没有产生新的对象")
print(lst)
lst[1:3]=[]        #用空列表来代替位点
print(lst)

运算结果:

切片删除操作——产生一个新的对象
[20, 40, 50, 60]
[20, 50]
切片删除操作——没有产生新的对象
[20, 40, 50, 60]
[20, 60]

4、clear():清空列表

print(lst)
lst.clear()   #清空列表元素
print(lst)

运行结果:

[20, 60]
[]

5、del:删除列表

del lst
print(lst)    #NameError: name 'lst' is not defined 报错,显示未被定义,因为已经被删除了 

三、列表元素的修改操作

  1. 为指定索引的元素赋予一个新值
  2. 为指定的切片赋予一个新值
lst=[10,20,30,40]
print(lst)
print(lst[2])

lst[2]=100
print(lst)
#使用切片批量修改
lst[1:3]=[300,400,500,600]
print(lst)

运行结果:

[10, 20, 30, 40]
30
[10, 20, 100, 40]
[10, 300, 400, 500, 600, 40]

四、列表元素的排序操作

  1. 调用sort()方法,列表中的所有元素默认按照从小到大的顺序进行排序,可以指定reverse=True进行降序排序
  2. 调用内置函数sorted(),可以指定reverse=True,进行降序排列,原排列不发生变化
#使用sort()方法进行排序,不会生成新的列表
lst=[20,40,10,98,54]
print('排序前:',lst,id(lst))

lst.sort()                   #默认为升序排序
print('排序后:',lst,id(lst))

lst=[20,40,10,98,54]
print('排序前:',lst,id(lst))
lst.sort(reverse=True)       #reverse=True时,即是设置为逆排序
print('排序后:',lst,id(lst))

#使用sorted()对列表进行排序,将会产生一个新的列表对象
lst=[20,40,10,98,54]
print('原列表:',lst)
new_lst=sorted(lst)
print(lst)
print(new_lst)
#指定关键字参数,实现列表元素的降序排列
desc_list=sorted(lst,reverse=True)
print(desc_list)

运算结果:

排序前: [20, 40, 10, 98, 54] 1963111628928
排序后: [10, 20, 40, 54, 98] 1963111628928
排序前: [20, 40, 10, 98, 54] 1963115518976
排序后: [98, 54, 40, 20, 10] 1963115518976
原列表: [20, 40, 10, 98, 54]
[20, 40, 10, 98, 54]
[10, 20, 40, 54, 98]
[98, 54, 40, 20, 10]

五、列表生成

#列表生成,也就是生成列表的公式

#生成一个一到八列表
lst=[i for i in range(1,9)]
print(lst)  #[1, 2, 3, 4, 5, 6, 7, 8]

#生成2,4,6,8,10的一个列表
lst=[i*2 for i in range(1,6)]
print(lst)  #[2, 4, 6, 8, 10]

  

原文地址:https://www.cnblogs.com/xiaoqing-ing/p/14977005.html