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

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

  • 发布: python培训
  • 来源:python干货资料
  • 2020-07-01 16:31:24
  • 阅读()
  • 分享
  • 手机端入口

Python中的列表内建了许多方法。在下文中,使用“L”代表一个列表,使用“x”代表方法的参数,以便说明列表的使用方法。

1 append()方法

列表的append()方法用于将一个项添加到列表的末尾,L.append(x)等价于L[len(L):] = [x]。

例如,使用append()方法分别将'cow'和'elephant'添加到animals列表的末尾:

  1. >>> animals = ['cat''dog''fish''dog'
  2. >>> animals.append('cow')   # 等价于animals[4:]=['cow'] 
  3. >>> animals 
  4. ['cat''dog''fish''dog''cow'
  5. >>> animals.append('elephant')   # 等价于animals[5:]=['elephant'] 
  6. >>> animals 
  7. ['cat''dog''fish''dog''cow''elephant'

2 ()方法

列表的()方法用于将一个项插入指定索引的前一个位置。L.(0, x)是将x插入列表的最前面,L.(len(L)), x)等价于L.append(x)。

例如,使用()方法分别将'cow'和'elephant'插入animals列表:

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

3 extend()方法

列表的extend()方法用于将可迭代对象的所有项追加到列表中。L.extend(iterable)等价于L[len(L):] = iterable。extend()和append()方法的区别是,extend()方法会将可迭代对象“展开”。

例如,分别使用append()方法和extend()方法在animals列表后面追加一个包含'cow'和'elephant'的列表:

  1. >>> animals = ['cat''dog''fish''dog'
  2. >>> animals.append(['cow''elephant'])   # 此处append()参数是一个列表 
  3. >>> animals 
  4. ['cat''dog''fish''dog', ['cow''elephant']] 
  5. >>> animals = ['cat''dog''fish''dog'
  6. >>> animals.extend(['cow''elephant'])   # 此处extend()参数也是一个列表 
  7. >>> animals 
  8. ['cat''dog''fish''dog''cow''elephant'

4 remove()方法

列表的remove()方法用于移除列表中指定值的项。L.remove(x)移除列表中第一个值为x的项。如果没有值为x的项,那么会抛出ValueError异常。

例如,使用remove()方法移除animals列表中值为'dog'的项:

  1. >>> animals = ['cat''dog''fish''dog'
  2. >>> animals.remove('dog'
  3. >>> animals 
  4. ['cat''fish''dog'
  5. >>> animals.remove('dog'
  6. >>> animals 
  7. ['cat''fish'
  8. >>> animals.remove('dog'
  9. Traceback (most recent call last): 
  10.   File "", line 1in  
  11. ValueError: list.remove(x): x not in list 

5 pop()方法

列表的pop()方法用于移除列表中指定位置的项,并返回它。如果没有指定位置,那么L.pop()移除并返回列表的最后一项。

例如,使用pop()方法移除animals列表中指定位置的项:

  1. >>> animals = ['cat''dog''fish''dog'
  2. >>> animals.pop() 
  3. 'dog' 
  4. >>> animals 
  5. ['cat''dog''fish'
  6. >>> animals.pop(2
  7. 'fish' 
  8. >>> animals 
  9. ['cat''dog'

在调用前面的列表方法后,并没有打印任何值,而pop()方法打印了“弹出”的值。包括append()、()、pop()在内的方法都是“原地操作”。原地操作(又称为就地操作)的方法只是修改了列表本身,并不返回修改后的列表。

在类型转换时使用的int()函数,str()函数都有返回值:

  1. >>> number = 123 
  2. >>> mystring = str(number)   # 将返回值赋给变量mystring 
  3. >>> mystring 
  4. '123' 

但是在使用“原地操作”时,大部分则不会有返回值,包括pop()方法也只是返回了被“弹出”的值,并没有返回修改后的列表:

  1. >>> animals = ['cat''dog''fish''dog'
  2. >>> new_animals = animals.append('cow'
  3. >>> print(new_animals) 
  4. None 

 

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

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

THE END  

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

1 您的年龄

2 您的学历

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

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

快速通道fast track

近期开班时间TIME