当前位置:网站首页>列表常用方法
列表常用方法
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~
边栏推荐
- AI目标分割能力,无需绿幕即可实现快速视频抠图
- FOFAHUB usage test
- Speed up your programs with bitwise operations
- Moonbeam and Project integration of the Galaxy, bring brand-new user experience for the community
- 个人博客系统项目测试
- Handwriting a blogging platform ~ Day 3
- C语言之插入字符简单练习
- 考完PMP学什么?前方软考等着你~
- Entry name 'org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt' collided
- Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案
猜你喜欢

【LeetCode Daily Question】——704. Binary Search

LeetCode刷题日记:LCP 03.机器人大冒险

永磁同步电机36问(三)——SVPWM代码实现

From 2023 onwards, these regions will be able to obtain a certificate with a score lower than 45 in the soft examination.

【 wheeled odometer 】

Remember a pit for gorm initialization

Handwritten Blog Platform ~ Day Two

oracle query scan full table and walk index

2022-08-01 mysql/stoonedb慢SQL-Q18分析

Fly propeller power space future PIE - Engine Engine build earth science
随机推荐
2022-07-30 mysql8执行慢SQL-Q17分析
『网易实习』周记(二)
菜刀webshell特征分析
2022年NPDP考完多久出成绩?怎么查询?
LeetCode刷题日记:74. 搜索二维矩阵
Personal blog system project test
【LeetCode每日一题】——103.二叉树的锯齿形层序遍历
bool框架::PosInGrid (const简历:关键点kp, int &posX, int诗句)
CodeTon Round 2 D. Magical Array 规律
Scheduled tasks for distributed applications in Golang
Redis Persistence - RDB and AOF
Remember a gorm transaction and debug to solve mysql deadlock
Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021: Interpretation
nacos启动报错,已配置数据库,单机启动
leetcode / anagram in string - some permutation of s1 string is a substring of s2
永磁同步电机36问(二)——机械量与电物理量如何转化?
BioVendor Human Club Cellular Protein (CC16) Elisa Kit Research Fields
【LeetCode Daily Question】——704. Binary Search
拼多多借力消博会推动国内农产品品牌升级 看齐国际精品农货
Constructor instance method inheritance of typescript37-class (extends)