当前位置:网站首页>列表常用方法
列表常用方法
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~
边栏推荐
- Speed up your programs with bitwise operations
- Hiring a WordPress Developer: 4 Practical Ways
- Redis Persistence - RDB and AOF
- openGauss切换后state状态显示不对
- Golang分布式应用之Redis
- 拼多多借力消博会推动国内农产品品牌升级 看齐国际精品农货
- Fly propeller power space future PIE - Engine Engine build earth science
- A good book for newcomers to the workplace
- oracle查询扫描全表和走索引
- Handwriting a blogging platform ~ Day 3
猜你喜欢
Garbage Collector CMS and G1
Win Go development kit installation configuration, GoLand configuration
【Unity入门计划】2D Game Kit:初步了解2D游戏组成
FOFAHUB usage test
数据链路层的数据传输
A good book for newcomers to the workplace
菜刀webshell特征分析
Talking about the "horizontal, vertical and vertical" development trend of domestic ERP
The first time I wrote a programming interview question for Niu Ke: input a string and return the letter with the most occurrences of the string
2022 Henan Youth Training League Game (3)
随机推荐
"NetEase Internship" Weekly Diary (1)
NIO‘s Sword(牛客多校赛)
Analysis of volatile principle
nacos startup error, the database has been configured, stand-alone startup
The Paddle Open Source Community Quarterly Report is here, everything you want to know is here
Safety (2)
[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree
2023年起,这些地区软考成绩低于45分也能拿证
[ORB_SLAM2] void Frame::ComputeImageBounds(const cv::Mat & imLeft)
LeetCode刷题日记:LCP 03.机器人大冒险
MySQL - CRUD operations
swift project, sqlcipher3 -> 4, cannot open legacy database is there a way to fix it
LeetCode 213. Robbery II (2022.08.01)
to-be-read list
【 wheeled odometer 】
Coding Experience Talk
使用DBeaver进行mysql数据备份与恢复
AI target segmentation capability for fast video cutout without green screen
个人博客系统项目测试
LeetCode brushing diary: 33. Search and rotate sorted array