首页 > 编程笔记 > Python笔记 阅读:163

Python list列表详解(图文并茂,新手必看)

序列是 Python 中最基本的数据结构,序列中的每个值都有对应的位置值,称为索引,第一个索引是 0,第二个索引是 1,以此类推。

Python有 6 个序列的内置类型,但最常见的是列表(list)和元组(tuple)。

列表可以进行的操作包括索引、切片、加、乘、检查成员。此外,Python 已经内置确定序列的长度以及确定最大和最小元素的方法。

列表是最常用的 Python 数据类型,它可以作为一个方括号内的逗号分隔值出现。列表的数据项不需要具有相同的类型。

创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可。如下所示:
list1 = ['Google', 'Runoob', 1997, 2000]
list2 = [1, 2, 3, 4, 5]
list3 = ["a", "b", "c", "d"]
list4 = ['red', 'green', 'blue', 'yellow', 'white', 'black']

Python访问列表中的值

与字符串的索引一样,列表索引从 0 开始,第二个索引是 1,以此类推。

通过索引列表可以进行截取、组合等操作,效果如下图所示:


图 1 列表索引

【实例 1】访问列表中的值。
list=['red','green','blue','yellow','white','black']
print(list[0])
print(list[1])
print(list[2])
运行程序,输出如下:

red
green
blue

使用下标索引来访问列表中的值,同样也可以使用方括号 [] 的形式截取字符,如下图所示:


图 2 截取字符

【实例 2】使用负数索引值截取。
list = ['Google', 'Runoob', "Zhihu", "Taobao", "Wiki"]

# 读取第二位
print ("list[1]: ", list[1])
# 从第二位开始(包含)截取到倒数第二位(不包含)
print ("list[1:-2]: ", list[1:-2])
运行程序,输出如下:

list[1]: Runoob
list[1:-2]: ['Runoob','Zhihu']

Python更新列表

可以对列表的数据项进行修改或更新,也可以使用 append() 方法来添加列表项。

append() 方法用于在列表末尾添加新的对象,调用格式为:
list.append(obj)
obj 为添加到列表末尾的对象。该方法无返回值,但是会修改原来的列表。

【实例 3】更新列表操作实例。
list = ['Google', 'Runoob', 1997, 2020]
print("第三个元素为:", list[2])
list[2] = 2021
print("更新后的第三个元素为:", list[2])
list1 = ['Google', 'Runoob', 'Taobao']
list1.append('Baidu')
print("更新后的列表:", list1)
运行程序,输出如下:

第三个元素为: 1997
更新后的第三个元素为: 2021
更新后的列表: ['Google', 'Runoob', 'Taobao', 'Baidu']


【实例 4】定义两个函数:一个用 extend() 方法,一个用 append() 方法。
# -*- coding: UTF-8 -*-
def changeextend(str):  # 定义函数
    "print string with extend"
    mylist.extend([40,50,60])
    print("Print string mylist:",mylist)
    return
def changeappend(str):  # 定义函数
    "print string with append"
    mylist.append([7,8,9])
    print("print string mylist:",mylist)
    return
mylist = [10,20,30]
changeextend(mylist)
print("print extend mylist:", mylist)
changeappend(mylist)
print("print append mylist:", mylist)
运行程序,输出如下:
Print string mylist: [10, 20, 30, 40, 50, 60]
print extend mylist: [10, 20, 30, 40, 50, 60]
print string mylist: [10, 20, 30, 40, 50, 60, [7, 8, 9]]
print append mylist: [10, 20, 30, 40, 50, 60, [7, 8, 9]]

Python删除列表元素

在 Python 中,可以使用 del 语句来删除列表的元素。

【实例 5】使用 del 语句删除列表元素。
list = ['Google','Runoob',1997,2020]
print ("原始列表:", list)
del list[2]
print ("删除第三个元素:", list)
运行程序,输出如下:

原始列表: ['Google','Runoob',1997,2020]
删除第三个元素:['Google','Runoob',2020]


此外,还可以使用 remove() 函数移除列表中某个值的第一个匹配项。函数的语法格式为:
list.remove(obj)
obj 为列表中要移除的对象。没有返回值但是会移除列表中的某个值的第一个匹配项。

【实例 6】使用 remove() 函数删除列表的匹配项。
list1 = ['Google', 'Jingdong', 'Taobao', 'Baidu']
list1.remove('Taobao')
print("列表现在为:", list1)
list1.remove('Baidu')
print("列表现在为:", list1)
运行程序,输出如下:

列表现在为: ['Google','Jingdong','Baidu']
列表现在为: ['Google','Jingdong']

Python查找列表元素

Python 中的列表(list)提供了 index() 和 count() 方法,它们都可以用来查找元素。

1) index()方法

index() 方法用来查找某个元素在列表中出现的位置(也就是索引),如果该元素不存在,则会导致 ValueError 错误,所以在查找之前最好使用 count() 方法判断一下。

index() 的语法格式为:
listname.index(obj, start, end)
listname 表示列表名称,obj 表示要查找的元素,start 表示起始位置,end 表示结束位置:
index() 方法会返回元素所在列表中的索引值。

【实例 7】应用 index() 方法查找列表元素。
nums = [40, 36, 89, 2, 36, 100, 7, -20.5, -999]
# 检索列表中的所有元素
print( nums.index(2) )
# 检索 3~7 的元素
print( nums.index(100, 3, 7) )
# 检索 4 之后的元素
print( nums.index(7, 4) )
# 检索一个不存在的元素
print( nums.index(55) )
运行程序,输出如下:

3
5
6
Traceback (most recent call last):
  File "script.py", line 9, in
    print( nums.index(55) )
ValueError: 55 is not in list

2) count()方法

count() 方法用来统计某个元素在列表中出现的次数,基本语法格式为:
listname.count(obj)
listname 代表列表名,obj 表示要统计的元素。

如果 count() 返回 0,就表示列表中不存在该元素,所以 count() 也可以用来判断列表中是否存在某个元素。

【实例 8】 使用 count() 方法统计某元素在列表中出现的次数。
nums = [40, 36, 89, 2, 36, 100, 7, -20.5, 36]
# 统计元素出现的次数
print("36 出现了%d次" % nums.count(36))
# 判断一个元素是否存在
if nums.count(100) :
    print("列表中存在 100 这个元素")
else:
    print("列表中不存在 100 这个元素")
运行程序,输出如下:

36 出现了3次
列表中存在 100 这个元素

相关文章