1、列表创建
创建一个列表,只要把用逗号(",")分割的不同数据项使用方括号[]括起来即可,列表的数据项不需要具有相同的类型,如:
listA = ['ALi' , 'Tencent' , 'Baidu' , 'BytesDance' , 24 , 'B' , 81.0]
列表索引从0开始,可以进行截取、组合等操作
2、访问列表中的值
使用下标索引的方式访问列表中的值,也可以使用方括号的形式截取列表的值
listA = ['ALi' , 'Tencent' , 'Baidu' , 'BytesDance' , 24 , 'B' , 81.0]
print('全部打印listA:' , listA)
print('打印listA中第二个字符:' , listA[1])
print('打印listA中第二个到第五个字符:' , listA[1:5])
输出:
全部打印listA: ['ALi', 'Tencent', 'Baidu', 'BytesDance', 24, 'B', 81.0]
打印listA中第二个字符: Tencent
打印listA中第二个到第五个字符: ['Tencent', 'Baidu', 'BytesDance', 24]
3、更新列表
可以对列表的数据项进行修改或更新,append()函数可实现列表数据增加
listA = ['ALi' , 'Tencent' , 'Baidu' , 'BytesDance' , 24 , 'B' , 81.0]
print('listA更新前:' , listA)
listA[5] = 'AB'
print('listA更新后:' , listA)
输出:
listA更新前: ['ALi', 'Tencent', 'Baidu', 'BytesDance', 24, 'B', 81.0]
listA更新后: ['ALi', 'Tencent', 'Baidu', 'BytesDance', 24, 'AB', 81.0]
4、list.append()函数增添列表元素
使用list.append()函数在列表最末尾添加数据项
语法:
list.append(obj)
参数:
obj -- 添加到列表末尾的对象
返回值:
该方法无返回值,但是会修改原来的列表
listA = ['ALi' , 'Tencent' , 'Baidu' , 'BytesDance' , 24 , 'B' , 81.0]
print('listAt添加前:' , listA)
listA.append('Apple')
print('listAt添加后:' , listA)
输出:
listAt添加前: ['ALi', 'Tencent', 'Baidu', 'BytesDance', 24, 'B', 81.0]
listAt添加后: ['ALi', 'Tencent', 'Baidu', 'BytesDance', 24, 'B', 81.0, 'Apple']
5、删除列表元素
使用del()函数删除列表中元素
listA = listA = ['ALi' , 'Tencent' , 'Baidu' , 'BytesDance' , 24 , 'B' , 81.0]
print ("删除前listA为:" , listA)
del listA[5]
print("删除后listA为:" , listA)
输出:
删除前listA为: ['ALi', 'Tencent', 'Baidu', 'BytesDance', 24, 'B', 81.0]
删除后listA为: ['ALi', 'Tencent', 'Baidu', 'BytesDance', 24, 81.0]
6、列表脚本操作符
列表表达式 | 说明 | 结果 |
len(list_A) | 列表A的长度 | 7 |
[1 ,2 , 3] + [4 , 5 , 6] | 实现列表的拼接组合 | [1 , 2 ,3 ,4 ,5 ,6] |
["Kobe"] * 2 | 列表重复输出 | ["Kobe" , "Kobe"] |
"K" in ["Kobe"] | 元素是否存在于列表中 | True |
for i in [1 ,2 ,3] : print( i ; end = '') | for循环输出 | 1 2 3 |
list_A = ['ALi' , 'Tencent' , 'Baidu' , 'BytesDance' , 24 , 'B' , 81.0]
list_B = ["NBA" , 'CBA']
print('list_A列表长度为:' , list_A)
print('list_A和list_B连接后为:' , list_A + list_B)
print('list_B列表重复输出3次:' , list_B * 3)
print("NBA是否在list_B内:" , "NBA" in list_B)
for i in list_A:
print(i)
输出:
list_A列表长度为: 7
list_A和list_B连接后为: ['ALi', 'Tencent', 'Baidu', 'BytesDance', 24, 'B', 81.0, 'NBA', 'CBA']
list_B列表重复输出3次: ['NBA', 'CBA', 'NBA', 'CBA', 'NBA', 'CBA']
NBA是否在list_B内: True
ALi
Tencent
Baidu
BytesDance
24
B
81.0
7、列表截取与拼接
列表list的截取与字符串操作类似
list_C = ['Google', 'Tencent', 'Taobao' , 'TianMao']
表达式 | 说明 | 结果 |
list_C[N] | 读取列表中第N + 1个字符 | list_C[3]输出:"TianMao" |
list_C[ -N] | 从右侧开始读取倒数第N个元素: count from the right | list_C[-3]输出:"Tencent" |
list_C[N : M ] | 输出第N + 1至第M个(不包含第M个)之间的字符 | list_C[1 : 3 ]输出:'Tencent', 'Taobao' |
list_C = ['Google', 'Tencent', 'Taobao' , 'TianMao']
print('读取list_C中第2 + 1个字符:' , list_C[2])
print("从右侧开始读取倒数第3个元素:" , list_C[-3])
print('输出第1至第4个(不包含第3个)之间的字符:' , list_C[1 : 3])
输出:
读取list_C中第2 + 1个字符: Taobao
从右侧开始读取倒数第3个元素: Tencent
输出第1至第4个(不包含第3个)之间的字符: ['Tencent', 'Taobao']
8、列表嵌套
列表嵌套即一个列表的元素是一个列表,如:
list_C = ['Google', 'Tencent', 'Taobao' , 'TianMao']
list_D = ['NBA' , 'CBA' , '世界杯' , '奥运会']
list_E = [list_C , list_D ] # list-C和list_D作为list_E的元素,实现列表嵌套
print("list_E是[list_C和list_D的嵌套:" , list_E)
print("list_E的第一个元素是一个列表,它是:" , list_E[0])
print("list_E[i][j]方法实现读取子列表中的值:" , list_E[1][1])
输出:
list_E是[list_C和list_D的嵌套: [['Google', 'Tencent', 'Taobao', 'TianMao'], ['NBA', 'CBA', '世界杯', '奥运会']]
list_E的第一个元素是一个列表,它是: ['Google', 'Tencent', 'Taobao', 'TianMao']
list_E[][]方法实现读取子列表中的值: CBA
9、列表函数/方法
假设:
列表list_C = ['Google', 'Tencent', 'Taobao' , 'TianMao']
元组seq_C = ('Google', 'Tencent', 'Taobao' , 'TianMao' , 'Apple')
字符串str_C = 'KobeBryant'
函数/方法 | 说明 | 例子 |
len(list) | 计算list长度 | len(list_C)返回:4 |
max(list) | 返回列表元素最大值 | max(list_C)返回:TianMao |
min(list) | 返回列表元素最小值 | min(list_C):Google |
list(seq) list(str) | 将元组/字符串转换为列表,元组用括号()括起来,列表用方括号[]括起来,元组的数据值不可更改 | list(seq_C)返回: ['Google', 'Tencent', 'Taobao', 'TianMao', 'Apple'] list(str_C)返回: ['K', 'o', 'b', 'e', 'B', 'r', 'y', 'a', 'n', 't'] |
list.append(obj) - 追加 | append() 方法用于在列表末尾添加新的对象 语法: list.append(obj) 参数: obj -- 添加到列表末尾的对象,可以是字符串、列表、元组、集合、字典 返回: 该方法无返回值,但是会修改原来的列表 | # append() 方法用于在列表末尾添加新的对象
list_C = ['Google', 'Tencent', 'Taobao' , 'TianMao']
list_C.append('BaiDu') # append() 方法用于在列表末尾添加新的对象 - 字符串
print("list.append(): list_C列表末尾添加一个新字符串对象BaiDu,添加完后新列表为:" , list_C)
list_C = ['Google', 'Tencent', 'Taobao' , 'TianMao']
list_C.append(seq_C) # append() 方法用于在列表末尾添加新的对象 - 元组
print("list.append(): list_C列表末尾添加一个新元组对象seq_C,添加完后新列表为:" , list_C)
list_C = ['Google', 'Tencent', 'Taobao' , 'TianMao']
list_C.append(['HuaWei' , 'OPPO']) # append() 方法用于在列表末尾添加新的对象 - 列表
print("list.append(): list_C列表末尾添加一个新列表对象list_C,添加完后新列表为:" , list_C)
list_C = ['Google', 'Tencent', 'Taobao' , 'TianMao']
list_C.append(set_C) # append() 方法用于在列表末尾添加新的对象 - 集合
print("list.append(): list_C列表末尾添加一个新集合对象set_C,添加完后新列表为:" , list_C)
list_C = ['Google', 'Tencent', 'Taobao' , 'TianMao']
list_C.append(dict_C) # append() 方法用于在列表末尾添加新的对象 - 字典
print("list.append(): list_C列表末尾添加一个新字典对象dict_C,添加完后新列表为:" , list_C)
list_C = ['Google', 'Tencent', 'Taobao' , 'TianMao']
print("list.append(): list_C列表末尾添加一个新字符串对象BaiDu,添加完后新列表为:" , list_C.append('BaiDu')) # list.append()方法无返回值,因此输出none 输出: list.append(): list_C列表末尾添加一个新字符串对象BaiDu,添加完后新列表为: ['Google', 'Tencent', 'Taobao', 'TianMao', 'BaiDu']
list.append(): list_C列表末尾添加一个新元组对象seq_C,添加完后新列表为: ['Google', 'Tencent', 'Taobao', 'TianMao', ('Google', 'Apple')]
list.append(): list_C列表末尾添加一个新列表对象list_C,添加完后新列表为: ['Google', 'Tencent', 'Taobao', 'TianMao', ['HuaWei', 'OPPO']]
list.append(): list_C列表末尾添加一个新集合对象set_C,添加完后新列表为: ['Google', 'Tencent', 'Taobao', 'TianMao', {'Kobe', 'Bryant'}]
list.append(): list_C列表末尾添加一个新字典对象dict_C,添加完后新列表为: ['Google', 'Tencent', 'Taobao', 'TianMao', {'Kobe': '24', 'Curry': '30'}]
list.append(): list_C列表末尾添加一个新字符串对象BaiDu,添加完后新列表为: None |
list.count(obj) | count() 方法用于统计某个元素在列表中出现的次数 语法: list.count(obj) 参数: obj -- 列表中统计的对象 返回值: 返回元素在列表中出现的次数 | list_C.count('Taobao')返回:1 |
list.extend(seq) - 拓展 | extend() 函数用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表) 语法: list.extend(seq) 参数: seq -- 元素列表,可以是字符串、列表、元组、集合、字典,若为字典,则仅会将键(key)作为元素依次添加至原列表的末尾 返回: 该方法没有返回值,但会在已存在的列表中添加新的列表内容 | # extend() 方法用于在列表末尾添加新的对象
list_C = ['Google', 'Tencent', 'Taobao' , 'TianMao']
list_C.extend('BaiDu') # append() 方法用于在列表末尾添加新的对象 - 字符串
print("list.extend(): list_C列表末尾添加一个新字符串对象BaiDu,添加完后新列表为:" , list_C)
list_C = ['Google', 'Tencent', 'Taobao' , 'TianMao']
list_C.extend(seq_C) # append() 方法用于在列表末尾添加新的对象 - 元组
print("list.extend(): list_C列表末尾添加一个新元组对象seq_C,添加完后新列表为:" , list_C)
list_C = ['Google', 'Tencent', 'Taobao' , 'TianMao']
list_C.extend(['HuaWei' , 'OPPO']) # append() 方法用于在列表末尾添加新的对象 - 列表
print("list.extend(): list_C列表末尾添加一个新列表对象list_C,添加完后新列表为:" , list_C)
list_C = ['Google', 'Tencent', 'Taobao' , 'TianMao']
list_C.extend(set_C) # append() 方法用于在列表末尾添加新的对象 - 集合
print("list.extend(): list_C列表末尾添加一个新集合对象set_C,添加完后新列表为:" , list_C)
list_C = ['Google', 'Tencent', 'Taobao' , 'TianMao']
list_C.extend(dict_C) # append() 方法用于在列表末尾添加新的对象 - 字典
print("list.extend(): list_C列表末尾添加一个新字典对象dict_C,添加完后新列表为:" , list_C)
list_C = ['Google', 'Tencent', 'Taobao' , 'TianMao']
print("list.extend(): list_C列表末尾添加一个新字符串对象BaiDu,添加完后新列表为:" , list_C.extend('BaiDu')) # list.extend()方法无返回值,因此输出none 输出: list.extend(): list_C列表末尾添加一个新字符串对象BaiDu,添加完后新列表为: ['Google', 'Tencent', 'Taobao', 'TianMao', 'B', 'a', 'i', 'D', 'u']
list.extend(): list_C列表末尾添加一个新元组对象seq_C,添加完后新列表为: ['Google', 'Tencent', 'Taobao', 'TianMao', 'Google', 'Apple']
list.extend(): list_C列表末尾添加一个新列表对象list_C,添加完后新列表为: ['Google', 'Tencent', 'Taobao', 'TianMao', 'HuaWei', 'OPPO']
list.extend(): list_C列表末尾添加一个新集合对象set_C,添加完后新列表为: ['Google', 'Tencent', 'Taobao', 'TianMao', 'Kobe', 'Bryant']
list.extend(): list_C列表末尾添加一个新字典对象dict_C,添加完后新列表为: ['Google', 'Tencent', 'Taobao', 'TianMao', 'Kobe', 'Curry']
list.extend(): list_C列表末尾添加一个新字符串对象BaiDu,添加完后新列表为: None |
list.index(obj) | index() 函数用于从列表中找出某个值第一个匹配项的索引位置 语法: list.index(x[, start[, end]]) 参数: x-- 查找的对象。 start-- 可选,查找的起始位置 end-- 可选,查找的结束位置 返回: 该方法返回查找对象的索引位置,如果没有找到对象则抛出异常 | # index() 函数用于从列表中找出某个值第一个匹配项的索引位置
list_A = ['Google', 'Tencent', 'Taobao', 'TianMao', 'Kobe', 'Bryant' , 'Curry']
print("list_A列表中Kobe的索引值为:" , list_A.index("Kobe"))
print("list_A列表中Kobe的索引值为:" , list_A.index("Kobe" , 0 ,len(list_A)))
print("list_A列表中Kobe的索引值为:" , list_A.index("Kobe" , 1 ,3)) #当查找不到时报错 输出: list_A列表中Kobe的索引值为: 4
list_A列表中Kobe的索引值为: 4
Traceback (most recent call last):
File "G:/Python/IDEAWorkspace/PythonProject/Practice/列表list[].py", line 154, in <module>
print("list_A列表中Kobe的索引值为:" , list_A.index("Kobe" , 1 ,3))
ValueError: 'Kobe' is not in list |
list.insert(index, obj) | insert() 函数用于将指定对象插入列表的指定位置 语法: list.insert(index, obj) 参数: index -- 对象obj需要插入的索引位置 obj -- 要插入列表中的对象,支持字符串、列表、元组、集合、字典 返回值: 该方法没有返回值,但会在列表指定位置插入对象 | # insert() 函数用于将指定对象插入列表的指定位置
list_A = ['Google', 'Tencent', 'Taobao', 'TianMao', 'Kobe', 'Bryant' , 'Curry']
print("list_A插入前列表为:" , list_A)
list_A.insert(list_A.index('Google') + 1 , ['Apple' ,'HuaWei'])
print("在list_A列表元素'Google'后插入['Apple' ,'HuaWei']:" , list_A)
list_A = ['Google', 'Tencent', 'Taobao', 'TianMao', 'Kobe', 'Bryant' , 'Curry']
list_A.insert(list_A.index('Google') , ['Apple' ,'HuaWei'])
print("在list_A列表元素'Google'前插入['Apple' ,'HuaWei']:" , list_A) 输出: list_A插入前列表为: ['Google', 'Tencent', 'Taobao', 'TianMao', 'Kobe', 'Bryant', 'Curry']
在list_A列表元素'Google'后插入['Apple' ,'HuaWei']: ['Google', ['Apple', 'HuaWei'], 'Tencent', 'Taobao', 'TianMao', 'Kobe', 'Bryant', 'Curry']
在list_A列表元素'Google'前插入['Apple' ,'HuaWei']: [['Apple', 'HuaWei'], 'Google', 'Tencent', 'Taobao', 'TianMao', 'Kobe', 'Bryant', 'Curry'] |
list.pop([index=-1]) | pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值 语法: list.pop([index=-1]) 参数: index -- 可选参数,要移除列表元素的索引值,不能超过列表总长度,默认为 index=-1,删除最后一个列表值 返回值: 该方法返回从列表中移除的元素对象 | # pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
list_A = ['Google', 'Tencent', 'Taobao', 'TianMao', 'Kobe', 'Bryant' , 'Curry']
print("默认移除list_A最后一个元素:" , list_A.pop())
print("移除list_A第二个元素:" , list_A.pop(1))
print("移除字符超过list_A范围时报错:" , list_A.pop(7)) 输出: 默认移除list_A最后一个元素: Curry
移除list_A第二个元素: Tencent
Traceback (most recent call last):
File "G:/Python/IDEAWorkspace/PythonProject/Practice/practice_3.py", line 73, in <module>
print("移除字符超过list_A范围时报错:" , list_A.pop(7))
IndexError: pop index out of range |
list.remove(obj) | remove() 函数用于移除列表中某个值的第一个匹配项 语法: list.remove(obj) 参数: obj -- 列表中要移除的对象 返回值: 该方法没有返回值但是会移除列表中的某个值的第一个匹配项 | # remove() 函数用于移除列表中某个值的第一个匹配项
list_A = ['Google', 'Tencent', 'Google' , 'Taobao', 'TianMao', 'Kobe', 'Bryant' , 'Curry']
print("list_A移除'Google'前为:" , list_A)
# 移除list_A中'Google'的第一个匹配项
list_A.remove('Google')
print("list_A移除'Google'后为:" , list_A) 输出: list_A移除'Google'前为: ['Google', 'Tencent', 'Google', 'Taobao', 'TianMao', 'Kobe', 'Bryant', 'Curry']
list_A移除'Google'后为: ['Tencent', 'Google', 'Taobao', 'TianMao', 'Kobe', 'Bryant', 'Curry'] |
list.reverse() | reverse() 函数用于反向列表中元素 语法: list.reverse() 返回值: 该方法没有返回值,但是会对列表的元素进行反向排序 | # reverse() 函数用于反向列表中元素
list_A = ['Google', 'Tencent', 'Google' , 'Taobao', 'TianMao', 'Kobe', 'Bryant' , 'Curry']
print("反向排序列表元素前:" , list_A)
list_A.reverse() # reverse() 函数用于反向排序列表元素
print("反向排序列表元素后:" , list_A)
print("reverse() 函数用于反向排序列表元素,其返回值为:" , list_A.reverse()) 输出: 反向排序列表元素前: ['Google', 'Tencent', 'Google', 'Taobao', 'TianMao', 'Kobe', 'Bryant', 'Curry']
反向排序列表元素后: ['Curry', 'Bryant', 'Kobe', 'TianMao', 'Taobao', 'Google', 'Tencent', 'Google']
reverse() 函数用于反向排序列表元素,其返回值为: None |
list.sort( key=None, reverse=False) | sort() 函数用于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数
语法:
list.sort( key=None,reverse=False)
参数: key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。 reverse -- 排序规则,reverse = True 降序, reverse = False 升序(默认) 返回值: 该方法没有返回值,但是会对列表的对象进行排序 | # sort() 函数用于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数;该方法没有返回值,但是会对列表的对象进行排序。
list_A = ['Google', 'Tencent','TianMao', 'Google' , 'Taobao', 'Kobe', 'Bryant' , 'Curry']
list_A.sort(reverse = True) #sort() 函数对list_A列表进行降序排列
print("sort() 函数对list_A列表进行降序排列reverse = True:" , list_A)
list_A = ['Google', 'Tencent','TianMao', 'Google' , 'Taobao', 'Kobe', 'Bryant' , 'Curry']
list_A.sort() #sort() 函数对list_A列表进行升序排列
print("sort() 函数对list_A列表进行升序排列reverse = False:" , list_A)
list_A = ['Google', 'Tencent','TianMao', 'Google' , 'Taobao', 'Kobe', 'Bryant' , 'Curry']
print('sort()方法没有返回值,但是会对列表的对象进行排序:' , list_A.sort())
# 通过指定列表中的元素排序
# 获取列表的第二个元素
def takeSecond(elem): # elem可以是任何一个具有切片方法的对象,例如列表、元组、字符串
return elem[1] # 返回切片的第二个元素作为排序依据
# 列表:元素为元组
list_num = [(2, 2), (3, 4), (4, 1), (1, 3)]
# 指定第二个元素排序
list_num.sort(key=takeSecond)
# 输出类别
print ('列表list_num以列表的第二个元素排序:', list_num) 输出: reverse() 函数用于反向排序列表元素,其返回值为: None
sort() 函数对list_A列表进行降序排列reverse = True: ['TianMao', 'Tencent', 'Taobao', 'Kobe', 'Google', 'Google', 'Curry', 'Bryant']
sort() 函数对list_A列表进行升序排列reverse = False: ['Bryant', 'Curry', 'Google', 'Google', 'Kobe', 'Taobao', 'Tencent', 'TianMao']
sort()方法没有返回值,但是会对列表的对象进行排序: None
列表list_num以列表的第二个元素排序: [(4, 1), (2, 2), (1, 3), (3, 4)] |
list.copy() | copy() 函数用于复制列表,类似于 a[:] 语法: list.copy() 返回值: 返回复制后的新列表 | # copy() 函数用于复制列表
list_A = ['Google', 'Tencent','TianMao', 'Google' , 'Taobao', 'Kobe', 'Bryant' , 'Curry']
print("list_A:" , list_A)
# 复制list_A
list_B = list_A.copy()
list_C = list_A[:]
print("list_B:" , list_B)
print("list_C:" , list_C) 输出: list_A: ['Google', 'Tencent', 'TianMao', 'Google', 'Taobao', 'Kobe', 'Bryant', 'Curry']
list_B: ['Google', 'Tencent', 'TianMao', 'Google', 'Taobao', 'Kobe', 'Bryant', 'Curry']
list_C: ['Google', 'Tencent', 'TianMao', 'Google', 'Taobao', 'Kobe', 'Bryant', 'Curry'] |
list.clear() | clear() 函数用于清空列表,类似于 del a[:] | # clear() 函数用于清空列表,类似于 del a[:]
list_A = ['Google', 'Tencent','TianMao', 'Google' , 'Taobao', 'Kobe', 'Bryant' , 'Curry']
print("list_A原为:" , list_A)
list_B = list_A.clear()
print("list_B:" , list_B) list_B: None  |