技巧 | Python 列表经典使用技巧

List(列表)是Python中使用最多的一种数据结构,如何高效操作列表是提高代码运行效率的关键,本期将介绍列表的几大经典操作技巧,希望对你有帮助。

1、List 添加元素

append():用于在列表的末尾追加任何数据类型的元素,被追加的元素在List中保持着原结构类型。

list1 = [1,2,3,4]
list1.append('python')
# list1 = [1, 2, 3, 4, 'python']

list2 = ['python','当打之年']
list1.append(list2)
# list1 = [1, 2, 3, 4, 'python', ['python', '当打之年']]

extend():将一个列表中每个元素依次分别添加到另一个列表中。

list1 = [1,2,3,4]
list2 = ['python','当打之年']
list1.extend(list2)
# list1 = [1, 2, 3, 4, 'python', '当打之年']

insert():将指定的对象插入到列表中指定的位置。

list1 = [1,2,3,4]
list2 = ['python','当打之年']
list1.insert(1,list2)
# list1 = [1, ['python', '当打之年'], 2, 3, 4]

2、List 删除元素

remove():按值删除,删除指定值元素,只会删除第一个和指定值相同的元素。

list1 = [1, 2, 3, 4, 'python', '当打之年', 'python']
list1.remove('python')
# list1 = [1, 2, 3, 4, '当打之年', 'python']

del(): 按索引删除,删除指定索引元素,可以删除单个索引元素,也可删除连续索引元素。

list1 = [1, 2, 3, 4, 'python', '当打之年', 'python']
del list1[1]
# list1 = [1, 3, 4, 'python', '当打之年', 'python']

list1 = [1, 2, 3, 4, 'python', '当打之年', 'python']
del list1[1:5]
# list1 = [1, '当打之年', 'python']

pop(): 按索引删除,删除指定索引元素,只可删除单个元素,若索引省略则删除最后一个元素(常用)。

list1 = [1, 2, 3, 4, 'python', '当打之年', 'python']
list1.pop(0)
# list1 = [2, 3, 4, 'python', '当打之年', 'python']
list1 = [1, 2, 3, 4, 'python', '当打之年', 'python']
list1.pop()
# list1 = [1, 2, 3, 4, 'python', '当打之年']

clear(): 删除列表所有元素。

list1 = [1, 2, 3, 4, 'python', '当打之年', 'python']
list1.clear()
# list1 = []

3、List 切片

语法:list[start: end: step],获取指定范围的子集,参数均可省略。

list1 = [1, 2, 3, 4, 'python', '当打之年', 'python']
list2 = list1[1:6:2]
# list2 = [2, 4, '当打之年']
list2 = list1[:6:2]
# list2 = [1, 3, 'python']
list2 = list1[1::2]
# list2 = [2, 4, '当打之年']
list2 = list1[1:6:]
# list2 = [2, 3, 4, 'python', '当打之年']
list2 = list1[::-1]
# list2 = ['python', '当打之年', 'python', 4, 3, 2, 1]

4、List 遍历

普通用法:

list1 = ['python', '当打之年', 'python']
for i in range(len(list1)):
    print(i, '--', list1[i])
# 0 -- python
# 1 -- 当打之年
# 2 -- python

高级用法:

list1 = ['python', '当打之年', 'python']
for index, data in enumerate(list1):
    print(index, '--',data)
# 0 -- python
# 1 -- 当打之年
# 2 -- python

5、随机获取 List 元素

random.choice()方法

import random
list1 = [1, 2, 3, 4, 'python', '当打之年', 'python']
data1 = random.choice(list1)
# 2
data2 = random.choice(list1)
# 当打之年
data3 = random.choice(list1)
# 3

6、List 排序

sort():在原列表内部进行排序

list1 = [5, 2, 3, 7, 0, 4, 3, 9, 6]
list2 = list1.sort()
# list1 = [0, 2, 3, 3, 4, 5, 6, 7, 9]
# list2 = None

sorted():内置函数,生成新的列表,不改变原列表

list1 = [5, 2, 3, 7, 0, 4, 3, 9, 6]
list2 = sorted(list1)
# list1 = [5, 2, 3, 7, 0, 4, 3, 9, 6]
# list2 = [0, 2, 3, 3, 4, 5, 6, 7, 9]

7、判断 List 是否为空

if list1 == []:
    print('空列表')
    
if len(list1) == 0:
    print('空列表')
    
if not list1:
    print('空列表')

8、列表推导式

列表式代码优雅,但一般比较复杂的程序建议少用(阅读性较差)

# 单一元素
list1 = [i*5 for i in range(5)]
print('list1 = ',list1)
# list1 = [0, 5, 10, 15, 20]

# 增加判断条件
list1 = [2, 4, 5, 6, 3]
list2 = [i*3 for i in list1 if i < 5]
print('list2 = ',list2)
# list2 = [6, 12, 9]

# 多重循环
list1= [1, 2, 3]
list2 =[4, 5, 6]
list3 = [x+y for x in list1 for y in list2]
# list3 = [5, 6, 7, 6, 7, 8, 7, 8, 9]

以上就是本期为大家整理的全部内容了,赶快练习起来吧,喜欢的朋友可以点赞、收藏也可以分享让更多人知道哦

更多内容:技巧 | Python 列表经典使用技巧

推荐阅读

技巧 | Python 字典用法详解(超全)
技巧 | 20个Pycharm最实用最高效的快捷键(动态展示)
可视化 | Python制作最炫3D可视化地图
可视化 | 动起来的中国大学排名,看看你的母校在哪里
可视化 | Python时间序列化NBA球星赛季数据

微信公众号 “Python当打之年” ,每天都有python编程技巧推送,希望大家可以喜欢
在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Python当打之年

您的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值