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高清壁纸批量下载(源码+工具)!

相关文章
|
3天前
|
存储 索引 Python
【Python列表解锁】:掌握序列精髓,驾驭动态数据集合
【Python列表解锁】:掌握序列精髓,驾驭动态数据集合
|
3天前
|
存储 索引 Python
Python零基础入门-5 数据结构(列表和元组
Python零基础入门-5 数据结构(列表和元组
|
3天前
|
索引 Python
Python零基础入门-2 数字、字符串和列表
Python零基础入门-2 数字、字符串和列表
|
4天前
|
vr&ar 索引 Python
Python基础教程(第3版)中文版 第二章列 表和元组(笔记)
Python基础教程(第3版)中文版 第二章列 表和元组(笔记)
|
6天前
|
Python
Python编程实战:如何将列表组装成一棵树结构
本文介绍了如何在Python中将列表转换为树结构。首先定义`TreeNode`类表示节点,包含值和子节点列表。然后,通过`list_to_tree`函数递归地将列表转为树。此外,还提供了添加和删除节点的方法。文章旨在帮助读者理解和操作树结构,以解决实际编程问题。
Python编程实战:如何将列表组装成一棵树结构
|
10天前
|
存储 供应链 开发者
Python列表打造简易进销存系统:轻松管理库存信息!
Python列表打造简易进销存系统:轻松管理库存信息!
|
10天前
|
存储 算法 数据处理
掌握Python列表:灵活存储、便捷操作,轻松处理各类数据
掌握Python列表:灵活存储、便捷操作,轻松处理各类数据
|
12天前
|
索引 Python
Python利用列表、字典和zip函数处理数据
最近重温Python基础语法,一道练习题巩固下列表、字典、循环。 给定下面两个列表 attributes 和 values,要求针对 values 中每一组子列表 value,输出其和 attributes 中的键对应后的字典,最后返回字典组成的列表,请分别用一行和多行条件循环语句,来实现这个功能
|
12天前
|
Python
【Python 训练营】N_15 列表元素去重
【Python 训练营】N_15 列表元素去重
11 1
|
12天前
|
Python
Python中列表和整数相乘
【6月更文挑战第3天】
11 4