1.创建列表
>>> list = [0,1,2,3,4,5]
>>> list
[0, 1, 2, 3, 4, 5]
>>> list1 = [x+1 for x in range(10) ]
>>> list1
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2.获取列表值
>>> list[0]
0
>>> list[0:3]
[0, 1, 2]
>>> len(list)
6
3.更新列表值
>>> list[0] = 'updata'
>>> list
['updata', 1, 2, 3, 4, 5]
4.删除列表值
>>> del list[0]
>>> list
[1, 2, 3, 4, 5]
5.函数和方法
函数 | 描述 |
len(list) | 列表元素个数 |
max(list) | 返回列表元素最大值 |
min(list) | 返回列表元素最小值 |
list(seq) | 将元组转换为列表 |
list.append(obj) |
在列表末尾添加新的对象 |
list.count(obj) |
统计某个元素在列表中出现的次数 |
list.extend(seq) |
在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表) |
list.index(obj) |
从列表中找出某个值第一个匹配项的索引位置 |
list.insert(index, obj) |
将对象插入列表 |
list.pop([index=-1]) |
移除列表中的一个元素(默认最后一个元素),并且返回该元素的值 |
list.remove(obj) |
移除列表中某个值的第一个匹配项 |
list.reverse() |
反向列表中元素 |
list.sort(cmp=None, key=None, reverse=False) |
对原列表进行排序 |
list.clear() |
清空列表 |
list.copy() |
复制列表 |
6.列表嵌套
>>> a = [1,2,3,4]
>>> b = [5,6,7,8]
>>> c = [a , b]
>>> a
[1, 2, 3, 4]
>>> b
[5, 6, 7, 8]
>>> c
[[1, 2, 3, 4], [5, 6, 7, 8]]