Python 列表经典使用技巧

简介: 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]

END


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


往期推荐

程序员必须掌握的十大排序算法(上)

程序员必须掌握的十大排序算法(下)

Python送你王者荣耀官网全套皮肤!!!

收藏|Unsplash高清壁纸批量下载(源码+工具)!

相关文章
|
27天前
|
缓存 监控 数据可视化
微店item_search - 根据关键词取商品列表深度分析及 Python 实现
微店item_search接口可根据关键词搜索商品,返回商品信息、价格、销量等数据,适用于电商检索、竞品分析及市场调研。接口需通过appkey与access_token认证,支持分页与排序功能,Python示例代码实现调用流程,助力商品数据高效获取与分析。
|
8天前
|
程序员 Python
Python列表推导式:简洁与高效的艺术
Python列表推导式:简洁与高效的艺术
199 99
|
3月前
|
测试技术 数据处理 Python
Python列表推导式:简洁高效的数据处理利器
Python列表推导式:简洁高效的数据处理利器
247 80
|
6天前
|
缓存 算法 数据安全/隐私保护
VVICitem_search - 根据关键词取关键词取商品列表接口深度分析及 Python 实现
VVIC item_search接口支持关键词搜索服装商品,提供价格、销量、供应商等数据,助力市场调研与采购决策。
|
9天前
|
自然语言处理 算法 数据安全/隐私保护
item_review - Lazada 商品评论列表接口深度分析及 Python 实现
Lazada商品评论接口(item_review)可获取东南亚多国用户评分、评论内容、购买属性等数据,助力卖家分析消费者偏好、优化产品与营销策略。
|
2月前
|
索引 Python 容器
[oeasy]python096_列表_计数函数_count
本教程详细介绍了Python中列表的计数方法`count`,包括其基本用法、与`len`函数的区别,以及如何结合索引操作查找和删除特定元素。同时探讨了字符串对象的`count`方法,并通过实例演示了如何统计字符出现次数。
63 7
|
1月前
|
安全 测试技术 数据处理
Python列表推导式进阶:从简洁代码到高效编程的10个核心技巧
列表推导式是Python中高效的数据处理工具,能将多行循环代码压缩为一行,提升代码可读性与执行效率。本文详解其基础语法、嵌套循环、条件表达式、函数融合、性能优化等进阶技巧,并结合实战案例与边界条件处理,帮助开发者写出更优雅、高效的Python代码。
116 0
|
2月前
|
测试技术 API 开发者
淘宝关键词搜索商品列表API接入指南(含Python示例)
淘宝关键词搜索商品列表API是淘宝开放平台的核心接口,支持通过关键词检索商品,适用于比价、选品、市场分析等场景。接口提供丰富的筛选与排序功能,返回结构化数据,含商品ID、标题、价格、销量等信息。开发者可使用Python调用,需注意频率限制与错误处理,建议先在沙箱环境测试。
|
1月前
|
存储 程序员 数据处理
Python列表基础操作全解析:从创建到灵活应用
本文深入浅出地讲解了Python列表的各类操作,从创建、增删改查到遍历与性能优化,内容详实且贴近实战,适合初学者快速掌握这一核心数据结构。
173 0

推荐镜像

更多