IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> python自学笔记——第三章:列表简介 -> 正文阅读

[Python知识库]python自学笔记——第三章:列表简介

作者:recommend-item-box type_blog clearfix
  • 列表由一系列按特定顺序排列的元素组成,列表通常包含多个元素,因此给列表制定一个表示复数的名称是个好主意
  • Python中,用方括号([])表示列表,并用逗号分隔其中的元素,需要注意的是Python在打印列表时会将方括号也同样打印出来
  • 当请求获取列表元素时,Python只返回元素而不包括方括号。
  • 在Python中,第一个列表元素的索引为0而不是1。负数索引例如:
    bicycles[-n]

    意为访问列表bicycles的倒数第n个元素

  • 修改、添加和删除元素

  1. 修改列表元素:要修改列表元素可指定列表名修改的元素的索引,在指定该元素的新值
    transportations = ['car' , 'bicycle' , 'motorcycle']
    print(transportations)  
    transportations[0] = 'train'
    print(transportations)  
    
    
    输出:
    ['car', 'bicycle', 'motorcycle']
    ['train', 'bicycle', 'motorcycle']
  2. ?在列表中添加元素
    1.在列表末尾添加元素,append:
    transportations = ['car' , 'bicycle' , 'motorcycle']
    print(transportations)
    transportations.append('train')
    print(transportations)
    
    输出:
    ['car', 'bicycle', 'motorcycle']
    ['car', 'bicycle', 'motorcycle', 'train']

    方法:可以先传建一个空列表,再使用一系列函数调用append()来添加元素,例如:

    transportations = []
    transportations.append('car')
    transportations.append('bicycle')
    transportations.append('motorcycle')

    这种方法很常见,因为经常要等到程序运行后,我们才知道用户要在程序中存储哪些数据。为控制用户,可以首先创建一个空列表,用于存储用户将要输入的值,然后将用户提供的每个新值附加到列表中

    2.在列表中插入元素
    在列表中任意位置添加新的元素,insert():
    transportations = ['car' , 'bicycle' , 'motorcycle']
    print(transportations)
    transportations.insert(1,'train')
    print(transportations)
    
    
    输出:
    ['car', 'bicycle', 'motorcycle']
    ['car', 'train', 'bicycle', 'motorcycle']

    注意:上述两种方法都是永久修改!!!

  3. 从列表中删除元素
    1.知道索引下的删除del,例如:
    transportations = ['car' , 'bicycle' , 'motorcycle']
    print(transportations)
    del transportation[1]
    print(transportations)
    
    输出:
    ['car', 'bicycle', 'motorcycle']
    ['car', 'motorcycle']

    当我们要将元素从列表中删除,并接着使用它的值,是可以用pop()方法删除列表末尾的的元素,并能够接着使用它

    transportations = ['car' , 'bicycle' , 'motorcycle']
    print(transportations)
    popped_transportation = transportation.pop()
    print(popped_transportation)
    print(transportations)
    
    输出:
    ['car', 'bicycle', 'motorcycle']
    motorcycle
    ['car', 'bicycle']

    事实上,若我们采用在pop()方法中指定参数的方式,可以删除列表中任意位置的元素,并接着使用。只需要在圆括号中指定要删除的元素索引即可,例如:

    transportations = ['car' , 'bicycle' , 'motorcycle']
    print(transportations)
    popped_transportation = transportations.pop(1)
    print(popped_transportation)
    print(transportations)
    
    输出:
    ['car', 'bicycle', 'motorcycle']
    bicycle
    ['car', 'motorcycle']

    如果我们不知道要删除元素的索引,而知道元素的值,此时我们可以用remove的方法,例如:

    transportations = ['car' , 'bicycle' , 'motorcycle']
    print(transportations)
    transportations.remove('bicycle')
    print(transportations)
    
    输出:
    ['car', 'bicycle', 'motorcycle']
    ['car', 'motorcycle']

    ? ?在上例中是采用参数直接传递的方式,当然,也可以通过变量来传递参数,例如:

    transportations = ['car' , 'bicycle' , 'motorcycle']
    print(transportations)
    whatiwanttodelete = 'bicycle'
    transportations.remove(whatiwanttodelete)
    print(transportations)

    也可以达到同样的效果,这样做的好处是我们可以像使用pop()一样,接着使用这个被删除的元素,但需要注意的是,这二者的实现原理是完全不一样的!注意:remove()只能删除第一个指定的值!如果删除的值在列表中出现多次,就需要使用循环来确保将每个值都删除!

  • 组织列表

  1. 使用方法sort()对列表永久排序
    1.按照字母顺序排列,例如:
    transportations = ['car' , 'bicycle' , 'motorcycle']
    print(transportations)
    transportations.sort()
    print(transportations)
    
    输出:
    ['car', 'bicycle', 'motorcycle']
    ['bicycle', 'car', 'motorcycle']
    
    2.按照与字母顺序相反的顺序排列元素,只需在使用sort()时传递参数reverse,使reverse = True即可
    transportations = ['car' , 'bicycle' , 'motorcycle']
    print(transportations)
    transportations.sort(reverse = True)
    print(transportations)
    
    输出:
    ['car', 'bicycle', 'motorcycle']
    ['motorcycle', 'car', 'bicycle']

    注意:对列表的修改是永久性的!!!

  2. 使用方法sorted()对列表临时排序

    transportations = ['car' , 'bicycle' , 'motorcycle']
    print(transportations)
    print(sorted(transportations))
    print(transportations)
    
    输出:
    ['car', 'bicycle', 'motorcycle']
    ['bicycle', 'car', 'motorcycle']
    ['car', 'bicycle', 'motorcycle']
    
    若要按照与字母顺序相反的顺序排列元素,只需在使用sorted()时传递参数reverse,使reverse = True即可

    可见,函数sorted()并没有改变列表元素的原始顺序,如果想通过sorted()永久改变列表,需要将调用sorted()后的列表传递给原始列表,如下:

    transportations = sorted(transportations)

    注意:在并非所有的值都是小写时,按字母顺序排列列表元素要复杂些。此时在决定排列顺序时,有多种解读大写字母的方式。

  3. 倒着打印列表元素reverse()

    transportations = ['car' , 'bicycle' , 'motorcycle']
    print(transportations)
    transportations.reverse()
    print(transportations)
    
    输出:
    ['car', 'bicycle', 'motorcycle']
    ['motorcycle', 'bicycle', 'car']
    

    注意:reverse()不是按照字母顺序颠倒顺序,而只是将列表元素反转而已!!!如果想要恢复到原来的顺序只需要再次调用reverse即可。

  4. 确定列表长度len()

    transportations = ['car' , 'bicycle' , 'motorcycle']
    print(transportations)
    print(len(transportations))
    
    输出:
    ['car', 'bicycle', 'motorcycle']
    3

  • 心得:当我们列表索引错误却找不到解决方法时,不妨先将列表打印出来。?

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-07-28 07:42:06  更:2021-07-28 07:44:32 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/24 14:47:12-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码