400-650-7353
您所在的位置:首页 > IT干货资料 > python > 【Python基础知识】Python中列表的方法(下)

【Python基础知识】Python中列表的方法(下)

  • 发布: python培训
  • 来源:python干货资料
  • 2020-09-25 11:51:52
  • 阅读()
  • 分享
  • 手机端入口

1. clear()方法

列表的clear()方法用于移除列表中的全部项。L.clear()等价于del L[:]。

例如,使用clear()方法移除animals列表中的全部项:

  1. >>> animals = ['cat''dog''fish''dog'
  2. >>> animals.clear() 
  3. >>> animals 
  4. [] 

2. count()方法

列表的count()方法用于返回指定值在列表中出现的次数:

例如,使用count()方法分别返回animals列表中'dog'、'cat'和'cow'出现的次数:

  1. >>> animals = ['cat''dog''fish''dog'
  2. >>> animals.count('dog'
  3. 2 
  4. >>> animals.count('cat'
  5. 1 
  6. >>> animals.count('cow'
  7. 0 

3. index()方法

列表的index()方法用于返回列表中指定值的项的从零开始的索引。L.index(x) 返回列表中第一个值为 x 的项的从零开始的索引。如果没有值为x的项,那么会抛出ValueError异常。

例如,使用index()方法分别返回animals列表中值为'dog'和'cow'的项的从零开始的索引:

  1. >>> animals = ['cat''dog''fish''dog'
  2. >>> animals.index('dog'
  3. 1 
  4. >>> animals.index('cow')   # 'cow'不在列表中 
  5. Traceback (most recent call last): 
  6.   File "", line 1in  
  7. ValueError: 'cow' is not in list 

可以指定查找索引项的的起始值,L.index(x, n)从列表的第n+1项开始查找:

  1. >>> animals.index('dog'2)   # 从第3项开始查找,这样就忽略了第一个'dog' 
  2. 3 

也可以同时指定查找索引项的起始值和结束值(包括起始值,不包括结束值),这样就会在该范围内查找:

  1. >>> animals.index('dog'24)   # 查找范围是第3项和第4项 
  2. 3 
  3. >>> animals.index('dog'23)  # 查找范围只有第3项,没有'dog' 
  4. Traceback (most recent call last): 
  5.   File "", line 1in  
  6. ValueError: 'dog' is not in list 

如果对Python开发感兴趣或者想要深入学习的现在可以免费领取学习大礼包哦(点击领取80G课程资料 备注:领资料)。

【Python基础知识】Python中列表的方法(下)

4 sort()方法

列表的sort()方法用于将列表排序。

例如,使用sort()方法将animals列表按字典顺序排序:

  1. >>> animals = ['cat''dog''fish''dog'
  2. >>> animals.sort() 
  3. >>> animals 
  4. ['cat''dog''dog''fish'

使用sort()方法将数字列表排序:

  1. >>> numbers = [314159
  2. >>> numbers.sort() 
  3. >>> numbers 
  4. [113459

sort方法还可以使用参数改变列表的排序规则,这需要使用自定义参数,将在第七章进行详细阐述。

5. reverse()方法

列表的reverse()方法用于反转整个列表。

例如,使用reverse()方法反转animals列表:

  1. >>> animals = ['cat''dog''fish''dog'
  2. >>> animals.reverse() 
  3. >>> animals 
  4. ['dog''fish''dog''cat'

6. copy()方法

列表的copy()方法用于返回一份列表的浅拷贝。L.copy()等价于L[:]。

要复制一份列表,最先想到的方法可能是将列表赋值给另一个变量,不过这是行不通的:

  1. >>> animals = ['cat''dog''fish''dog'
  2. >>> animals_copy = animals 
  3. >>> animals_copy 
  4. ['cat''dog''fish''dog'
  5. >>> animals.append('cow'
  6. >>> animals 
  7. ['cat''dog''fish''dog''cow'
  8. >>> animals_copy 
  9. ['cat''dog''fish''dog''cow'

运行结果显然不符合预期。简单的“复制”只是给当前列表取了一个别名,用一个名称修改列表的内容,也会影响到用其他名字显示的列表。

使用copy()方法复制animals列表:

  1. >>> animals = ['cat''dog''fish''dog'
  2. >>> real_animals_copy = animals.copy() 
  3. >>> real_animals_copy 
  4. ['cat''dog''fish''dog'
  5. >>> animals.append('cow'
  6. >>> real_animals_copy.append('elephant'
  7. >>> animals 
  8. ['cat''dog''fish''dog''cow'
  9. >>> real_animals_copy 
  10. ['cat''dog''fish''dog''elephant'

运行结果符合预期,原始列表没有影响到备份列表,备份列表也没有影响到原始列表。

文章“【Python基础知识】Python中列表的方法(下)”已帮助

>>本文地址:https://www.ujiuye.com/zhuanye/2020/56482.html

THE END  

声明:本站稿件版权均属中公教育优就业所有,未经许可不得擅自转载。

1 您的年龄

2 您的学历

3 您更想做哪个方向的工作?

获取测试结果
  • 大前端大前端
  • 大数据大数据
  • 互联网营销互联网营销
  • JavaJava
  • Linux云计算Linux
  • Python+人工智能Python
  • 嵌入式物联网嵌入式
  • 全域电商运营全域电商运营
  • 软件测试软件测试
  • 室内设计室内设计
  • 平面设计平面设计
  • 电商设计电商设计
  • 网页设计网页设计
  • 全链路UI/UE设计UI设计
  • VR/AR游戏开发VR/AR
  • 网络安全网络安全
  • 新媒体与短视频运营新媒体
  • 直播带货直播带货
  • 智能机器人软件开发智能机器人
 

快速通道fast track

近期开班时间TIME