当前位置:网站首页>列表常用方法
列表常用方法
2022-08-02 02:18:00 【honker707】
作者简介:大家好我是hacker707,大家可以叫我hacker
个人主页:hacker707的csdn博客
系列专栏:python基础教程
推荐一款模拟面试、刷题神器点击跳转进入网站
python基础之列表常用方法
持续更新python基础知识,欢迎各位来访~🥳🥳🥳
列表
列表是什么?
列表由一系列特定顺序排列的元素组成,你可以创建包含字母表中的所有字母、数字0~9、所有家庭成员姓名的列表等等,也可以将任何东西放入列表中,其中元素之间可以没有任何关系,鉴于列表通常包含多个元素,给列表指定一个表示复数的名称(如names、digits或letters)是个不错的主意
在python中,列表用方括号[ ]表示,并用逗号分隔其中的元素。
列表常用方法
1.append()
定义 append() 方法向列表末尾追加元素。
举个栗子向fruits列表添加元素
fruits = ['apple', 'banana', 'cherry']
fruits.append("orange")
print(fruits)
运行结果如下:
['apple', 'banana', 'cherry', 'orange']
2.clear()
定义 clear()方法清空列表所有元素
举个栗子清空fruits所有元素(返回空列表)
fruits = ['apple', 'banana', 'cherry', 'orange']
fruits.clear()
print(fruits)
运行结果如下:
[]
3.copy()
定义 copy()方法返回指定列表的副本(复制列表)
举个栗子复制fruits列表
fruits = ['apple', 'banana', 'cherry', 'orange']
c = fruits.copy()
print(c)
运行结果如下:
['apple', 'banana', 'cherry', 'orange']
4.count()
定义 count()方法返回元素出现次数
举个栗子 返回 “cherry” 在 fruits 列表中出现的次数
fruits = ['apple', 'banana', 'cherry']
number = fruits.count("cherry")
print(number)
运行结果如下:
1
5.extend()
定义 extend()方法将列表元素(或任何可迭代的元素)添加到当前列表的末尾
举个栗子 把cars中的元素添加到fruits列表
fruits = ['apple', 'banana', 'cherry']
cars = ['Porsche', 'BMW', 'Volvo']
fruits.extend(cars)
print(fruits)
运行结果如下:
['apple', 'banana', 'cherry', 'Porsche', 'BMW', 'Volvo']
6.index()
定义 index()方法返回该元素最小索引值(找不到元素会报错)
举个栗子返回“cherry”元素的最小索引值
fruits = ['apple', 'banana', 'cherry']
x = fruits.index("cherry")
print(x)
运行结果如下:
2
7.insert()
定义 在指定位置插入元素
举个栗子将"orange"元素插入到fruits列表索引为1的位置
fruits = ['apple', 'banana', 'cherry']
fruits.insert(1, "orange")
print(fruits)
运行结果如下:
['apple', 'orange', 'banana', 'cherry']
8.reverse()
定义reverse() 方法反转元素的排序顺序
举个栗子反转fruits列表
fruits = ['apple', 'banana', 'cherry']
fruits.reverse()
print(fruits)
运行结果如下:
['cherry', 'banana', 'apple']
9.remove()
定义 remove() 方法具有指定值的首个元素
举个栗子删除 fruits 列表的 “banana” 元素
fruits = ['apple', 'banana', 'cherry']
fruits.remove("banana")
print(fruits)
运行结果如下:
['apple', 'cherry']
10.pop()
定义 pop() 删除指定位置的元素
举个栗子删除 fruits 列表的"banana"元素(指定该元素索引)
fruits = ['apple', 'banana', 'cherry']
fruits.pop(1)
print(fruits)
运行结果如下:
['apple', 'cherry']
11.sort()
定义 默认情况下,sort() 方法对列表进行升序排序
举个栗子以字母顺序排序cars列表
cars = ['Porsche', 'BMW', 'Volvo']
cars.sort()
print(cars)
运行结果如下:
['BMW', 'Porsche', 'Volvo']
扩展 reverse=True 可将对列表进行降序排序。默认是 reverse=False
举个栗子对cars列表进行降序排序
cars = ['Porsche', 'BMW', 'Volvo']
cars.sort(reverse=True)
print(cars)
运行结果如下:
['Volvo', 'Porsche', 'BMW']
以上就是列表常用的方法整理,如果有改进的建议欢迎私信或者在评论区留言奥~
欢迎各位来访,一起交流学习python~
边栏推荐
- Pinduoduo leverages the consumer expo to promote the upgrading of domestic agricultural products brands and keep pace with international high-quality agricultural products
- 2022-08-01 反思
- 个人博客系统项目测试
- 永磁同步电机36问(三)——SVPWM代码实现
- The Paddle Open Source Community Quarterly Report is here, everything you want to know is here
- 使用DBeaver进行mysql数据备份与恢复
- Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒
- Nanoprobes免疫测定丨FluoroNanogold试剂免疫染色方案
- Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案
- 哈希冲突和一致性哈希
猜你喜欢
volatile原理解析
Scheduled tasks for distributed applications in Golang
AWR分析报告问题求助:SQL如何可以从哪几个方面优化?
Handwriting a blogging platform ~ Day 3
MySQL8 download, start, configure, verify
nacos启动报错,已配置数据库,单机启动
oracle query scan full table and walk index
BI-SQL丨WHILE
Pinduoduo leverages the consumer expo to promote the upgrading of domestic agricultural products brands and keep pace with international high-quality agricultural products
AI目标分割能力,无需绿幕即可实现快速视频抠图
随机推荐
NIO's Sword
Oracle19c安装图文教程
What to study after the PMP exam?The soft exam ahead is waiting for you~
十字光标太小怎么调节、CAD梦想画图算量技巧
考完PMP学什么?前方软考等着你~
Project Background Technology Express
『网易实习』周记(一)
Handwritten Blog Platform ~ Day Two
How to adjust the cross cursor too small, CAD dream drawing calculation skills
Safety (2)
LeetCode Brushing Diary: 74. Searching 2D Matrix
LeetCode brushing diary: 53, the largest sub-array and
软件测试 接口自动化测试 pytest框架封装 requests库 封装统一请求和多个基础路径处理 接口关联封装 测试用例写在yaml文件中 数据热加载(动态参数) 断言
密码学的基础:X.690和对应的BER CER DER编码
AWR analysis report questions for help: How can SQL be optimized from what aspects?
【web】理解 Cookie 和 Session 机制
Use DBeaver for mysql data backup and recovery
Hiring a WordPress Developer: 4 Practical Ways
【LeetCode每日一题】——103.二叉树的锯齿形层序遍历
Effects of Scraping and Aggregation