python 中列表 (list) 的超详细说明
列表(list)
类似C语言中的数组
一、定义一个列表(4种)
中括号括起来,元素用逗号隔开
定义一个空列表:
list = []
定义一个非空列表 :
list = [1,2,3]
定义一个一行放不下的列表:
list = ['good','bad','fat',
'thin','fast']
直接定义多维列表:
heroInfos =[['魏','曹操',160.3],['蜀','刘备',177.1],['吴','周瑜',188.6]]
间接定义多维列表(列表嵌套):
words = ['a', 'b', 'c']
nums = [1, 2, 3]
x = [words, nums]
——————————————————————————————————————————————————————————————————
#输出结果为:
[['a', 'b', 'c'], [1, 2, 3]]
二、列表中元素可以包含什么?
列表的数据项不需要具有相同的类型
list1 = [1, 2, 3, 4, 5 ] #数值
list2 = ["a", "b", "c", "d"] #字符串(不涉及嵌套的话,单引号、双引号都行)
list3 = ['Google', 'Runoob', 1997, 2000] #字符串和数值
三、访问
与字符串的索引一样,列表索引从 0 开始,第二个索引是 1,依此类推。
要使用
[]
包裹索引数。
正向索引:
list = ['red', 'green', 'blue', 'yellow', 'white', 'black']
print(list[0])
print(list[1])
print(list[len(list)-1]) #列表长度-1,即最后一个元素的索引index
——————————————————————————————————————————————————————————————————
#输出结果为:
red
green
black
反向索引:
反向索引从 -1 开始,最后一个为 -1,倒数第二个为 -2 ,以此类推。
list = ['red', 'green', 'blue', 'yellow', 'white', 'black']
print(list[-1])
print(list[-2])
——————————————————————————————————————————————————————————————————
#输出结果为:
black
white
截取:
list_new = list[start:end]
结果也是一个列表!!
左闭右开,能取到 list[start],但是取不到 list[end],成员数:end-start。
①正向索引截取:
nums = [10, 20, 30, 40, 50, 60, 70, 80, 90]
print(nums[0:4])
——————————————————————————————————————————————————————————————————
#输出结果为:
[10, 20, 30, 40]
②反向索引截取:
也是按照数学上负数的大小,左小右大。
nums = [10, 20, 30, 40, 50, 60, 70, 80, 90]
print(nums[-3:-1])
——————————————————————————————————————————————————————————————————
#输出结果为:
[70, 80]
③混合正向反向索引截取:
nums = [10, 20, 30, 40, 50, 60, 70, 80, 90]
print(nums[1:-2])
——————————————————————————————————————————————————————————————————
#输出结果为:
[20, 30, 40, 50, 60, 70]
多维列表访问:
list = [['human','man',14],['animal','dog',2]]
print(list[0][1])
——————————————————————————————————————————————————————————————————
#输出结果为:
man
list = [['human','man',14],['animal','dog',2]]
print(list[0])
——————————————————————————————————————————————————————————————————
#输出结果为:
['human', 'man', 14]
如果要把列表和字符串放在一块打印输出,那么要用str()
包裹列表,将它转换为字符串类型。
list = [['human','man'],['animal','dog']]
print(str(list)+'good') # print(list+'good') ×
——————————————————————————————————————————————————————————————————
#输出结果为:
[['human', 'man'], ['animal', 'dog']]good
四、增、插、删、改、其他
列表新增之追加——append()
append是在列表的最后,追加一个元素。
list = ['first','second','third']
list.append('fourth')
print(list)
——————————————————————————————————————————————————————————————————
#输出结果为:
['first', 'second', 'third', 'fourth']
这里用到了符号点·
,符号点的意思是使用(点)前面变量的方法。
列表新增之插入——insert()
list.insert(index,value)
插入后,list(index) 即为 value 。原 list[index:end] 均往后挪一位。
每一次插入操作后,数组中元素的下标都会发生变化,要注意!
示例:
heros = ['牛魔', '妲己', '兰陵王']
heros.insert(2,'亚瑟')
print(heros)
——————————————————————————————————————————————————————————————————
#输出结果为:
['牛魔', '妲己', '亚瑟', '兰陵王']
列表删除——pop()
popvalue = list.pop(index)
移除列表中的指定元素(默认最后一个元素),并且返回该元素的值。
heros = ['牛魔', '妲己','亚瑟','兰陵王']
leave = heros.pop()
print(heros) # ['牛魔', '妲己','亚瑟']
heros.append('孙悟空') # ['牛魔', '妲己','亚瑟','孙悟空']
leave = heros.pop(1)
print(heros)
——————————————————————————————————————————————————————————————————
#输出结果为:
['牛魔', '妲己', '亚瑟']
['牛魔', '亚瑟', '孙悟空']
也可以直接使用 del 语句来删除列表中的元素:
del list[index]
列表改:
用不到什么指定方法,直接改就行:
heros = ['牛魔', '妲己','亚瑟','兰陵王']
heros[0]='孙尚香'
print(heros)
——————————————————————————————————————————————————————————————————
#输出结果为:
['孙尚香', '妲己', '亚瑟', '兰陵王']
其他:
序号 | 方法 |
---|---|
1 | list.count(obj) 统计某个元素在列表中出现的次数 |
2 | list.extend(seq) 在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表) |
3 | list.index(obj) 从列表中找出某个值第一个匹配项的索引位置 |
4 | list.remove(obj) 移除列表中某个值的第一个匹配项 |
5 | list.reverse() 使列表中元素顺序翻转过来 |
6 | list.sort( key=None, reverse=False) 对原列表进行排序,reverse=False 则从小到大排序,reverse=True 则从大到小排序。 对于非数值列表的排序,建议去GPT搜搜:是按照字符串中字符的 Unicode 码点值进行逐字符比较排序的。 |
7 | list.clear() 清空列表 |
8 | list.copy() 复制列表 |
脚本操作符
列表中, +
和 *
的操作符与字符串相似。+
号用于组合列表,*
号用于重复列表。
python表达式 | 结果 | 描述 |
---|---|---|
[1, 2, 3] + [4, 5, 6] | [1, 2, 3, 4, 5, 6] | 表内组合 |
[‘Hi!’] * 4 | [‘Hi!’, ‘Hi!’, ‘Hi!’, ‘Hi!’] | 表内重复 |
3 in [1, 2, 3] | True | 查询元素是否存在于列表中 |
五、列表有关函数
序号 | 函数 |
---|---|
1 | len(list) 返回列表元素个数 |
2 | max(list) 返回列表元素最大值 |
3 | min(list) 返回列表元素最小值 |
4 | list(tuple) 将元组转换为列表 运行之后,其中 tuple 仍是元组,list1=list(aTuple)才是列表 |
六、列表比较
列表比较需要引入 operator 模块的 eq 方法:
operator.eq(list1,list2)
两个列表相同,就会输出 True,不同就会输出 False 。
# 导入 operator 模块
import operator
a = [1,2]
b = [2,3]
c = [2,3]
print(operator.eq(a,b))
print(operator.eq(c,b))
——————————————————————————————————————————————————————————————————
#输出结果为:
False
True
作者:炸膛坦客