笔记出处(学习UP主视频记录) https://www.bilibili.com/video/av35698354?p=5

3.2.3 从列表中删除元素-使用del语句删除元素

motorcycles = ['honda', 'yamaha', 'suzuki']
print (motorcycles) del motorcycles[0]
print (motorcycles)

['honda', 'yamaha', 'suzuki']
['yamaha', 'suzuki']

motorcycles = ['honda', 'yamaha', 'suzuki']
print (motorcycles) del motorcycles[1]
print (motorcycles)

['honda', 'yamaha', 'suzuki']
['honda', 'suzuki']

3.2.3 从列表中删除元素-使用方法pop()删除元素

motorcycles = ['honda', 'yamaha', 'suzuki']
print (motorcycles) poped_motorcycle = motorcycles.pop()
print (motorcycles) print (poped_motorcycle)

['honda', 'yamaha', 'suzuki']
['honda', 'yamaha']
suzuki

3.2.3 从列表中删除元素-弹出列表中任何位置处的元素

motorcycles = ['honda', 'yamaha', 'suzuki']

first_owned = motorcycles.pop(0)
print ('The first motorcycle I owned was a ' + first_owned.title() + '.')

The first motorcycle I owned was a Honda.

3.2.3 从列表中删除元素-根据值删除元素

motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
print (motorcycles) motorcycles.remove('ducati')
print (motorcycles)

['honda', 'yamaha', 'suzuki', 'ducati']
['honda', 'yamaha', 'suzuki']

3.3 组织列表

3.3.1 方法sort()对列表进行永久性排序

cars = ['bmw', 'audi', 'toyota', 'subary']

cars.sort()
print (cars)

['audi', 'bmw', 'subary', 'toyota']

cars = ['bmw', 'audi', 'toyota', 'subary']

cars.sort(reverse=True)
print (cars)

['toyota', 'subary', 'bmw', 'audi']

3.3.2 函数sorted()对列表进行临时排序

cars = ['bmw', 'audi', 'toyota', 'subary']

print ("Here is the origin list: ")
print (cars) print ("\nHere is the sorted list: ")
print (sorted(cars)) print ("\nHere is the original list again: ")
print (cars)

Here is the origin list:
['bmw', 'audi', 'toyota', 'subary']

Here is the sorted list:
['audi', 'bmw', 'subary', 'toyota']

Here is the original list again:
['bmw', 'audi', 'toyota', 'subary']

3.3.3 方法reverse()倒着打印列表

cars = ['bmw', 'audi', 'toyota', 'subary']
print (cars) cars.reverse()
print (cars)

['bmw', 'audi', 'toyota', 'subary']
['subary', 'toyota', 'audi', 'bmw']

3.3.4 函数len()确定列表的长度

cars = ['bmw', 'audi', 'toyota', 'subary']

print (len(cars))

4

3.4 使用列表时避免索引错误

motorcycles = ['honda', 'yamaha', 'suzuki']
print (motorcycles[3])

print (motorcycles[3])
IndexError: list index out of range

最新文章

  1. EF jsonignore
  2. [.net 面向对象程序设计进阶] (9) 序列化(Serialization) (一) 二进制流序列化
  3. java字符串拼接与性能
  4. eclipse maven新建springMVC项目(原创)
  5. TIME_WAIT是什么?http连接
  6. cdoj 题目简单分类
  7. python与编码方式
  8. Java初转型-Tomcat安装和配置
  9. jar 命令使用详解
  10. adb -s 设备名 设备名还有非法字符
  11. 刨根问底:if 后怎么就可以跟对象,变量交换写法是语法糖吗?
  12. org.hibernate.HibernateException: Duplicate identifier in table for: Waa
  13. [转载]linux下网卡漂移导致网络不可用
  14. 四、u-boot 链接脚本
  15. C#实现office文档转换为PDF格式
  16. Hadoop大数据分析应用场景
  17. AIO + ByteBufferQueue + allocateDirect 终于可以与NIO的并发性能达到一致。
  18. 不以main为入口的函数
  19. ACM-ICPC (10/12)
  20. Computer Science: the Big Picture

热门文章

  1. Ribbon XML Editor 2019.01.23-Setup.zip(支持64位)
  2. 区别 new function(){} 和 function(){}()
  3. [LC] 15. 3Sum
  4. Jenkins+maven+jmeter+eclipse搭建自动化测试平台
  5. JsonPath入门教程
  6. JAVA数据结构——单链表
  7. HttpClient GET和POST请求
  8. RSA算法原理(简单易懂)
  9. python开发时小问题之端口占用
  10. win7+centos6.5安装双系统