序列是Python中最基本的数据结构。
序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推。
Python有6个序列的内置类型,但最常见的是列表和元组
序列都可以进行的操作包括索引,切片,加,乘,检查成员。
此外,Python已经内置确定序列的长度以及确定最大和最小的元素的方法。
列表是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现
列表的数据项不需要具有相同的类型
创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可
list1 = ['Google', 'run', 1997, 2000]与字符串的索引一样,列表索引从0开始。列表可以进行截取、组合等
list2 = [1, 2, 3, 4, 5]
list3 = ["a", "b", "c", "d"]
访问列表中的值
使用下标索引来访问列表中的值,同样你也可以使用方括号的形式截取字符
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
list1 = ['Google', 'run', 1997, 2000]
list2 = [1, 2, 3, 4, 5, 6, 7]
print('list1[0] :', list1[0])
print('list2[1:5]:', list2[1:5])
更新列表
对列表的数据项进行修改或更新,也可以使用append()方法来添加列表项
list = ['Google', 'run', 1997, 2010]
print('第三个元素为 :', list[2])
list[2] = 2001
print("更新后的第三个元素为:", list[2])
删除列表元素
可以使用 del 语句来删除列表的的元素,也可以使用remove() 方法
list3 = ['Google', 'run', 1997, 2010]
print('原始列表 :', list3)
del list3[2]
print('删除第三个元素:',list3)
Python列表脚本操作符
列表对 + 和 * 的操作符与字符串相似。+ 号用于组合列表,* 号用于重复列表
Python 表达式 | 结果 | 描述 |
---|---|---|
len([1, 2, 3]) | 3 | 长度 |
[1, 2, 3] + [4, 5, 6] | [1, 2, 3, 4, 5, 6] | 组合 |
['Hi!'] * 4 | ['Hi!', 'Hi!', 'Hi!', 'Hi!'] | 重复 |
3 in [1, 2, 3] | True | 元素是否存在于列表中 |
for x in [1, 2, 3]: print(x, end=" ") | 1 2 3 | 迭代 |
Python列表截取与拼接
Python的列表截取与字符串操作类型
L=['Google', 'Runoob', 'Taobao']
Python 表达式 | 结果 | 描述 |
---|---|---|
L[2] | 'Taobao' | 读取第三个元素 |
L[-2] | 'Runoob' | 从右侧开始读取倒数第二个元素: count from the right |
L[1:] | ['Runoob', 'Taobao'] | 输出从第二个元素开始后的所有元素 |
嵌套列表
使用嵌套列表即在列表里创建其它列表
a = ['a', 'b', 'c']输出
n = [1, 2, 3]
x = [a, n]
print(x)
print(x[0])
print(x[0][1])
[['a', 'b', 'c'], [1, 2, 3]]
['a', 'b', 'c']
b
Python列表函数&方法
Python包含以下函数
Python包含以下方法
序号 | 方法 |
---|---|
1 | list.append(obj) 在列表末尾添加新的对象 |
2 | list.count(obj) 统计某个元素在列表中出现的次数 |
3 | list.extend(seq) 在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表) |
4 | list.index(obj) 从列表中找出某个值第一个匹配项的索引位置 |
5 | list.insert(index, obj) 将对象插入列表 |
6 | list.pop([index=-1]) 移除列表中的一个元素(默认最后一个元素),并且返回该元素的值 |
7 | list.remove(obj) 移除列表中某个值的第一个匹配项 |
8 | list.reverse() 反向列表中元素 |
9 | list.sort( key=None, reverse=False) 对原列表进行排序 |
10 | list.clear() 清空列表 |
11 | list.copy() 复制列表 |
小结:
python 创建二维列表,将需要的参数写入 cols 和 rows 即可
list_2d = [[0 for col in range(cols)] for row in range(rows)]
空列表可以简单地通过两个中括号进行表示([])- 一里面什么东西都没有,
但是,如果想创建一个占用十个元素空间,却不包括任何有用内容的列表
可以类似于下面的方法,用某一个具体的值代替
list_empty = [0]*10
这样就生成了一一个包括10个0的列表
可能会需要一个值来代表空一意味着没有在里面放置任何元素。这个时候就需要使用None。None 是一个Python的内建值,它的确切含意是"这里什么也没有"。因此,如果想初始化个长度为10的列表,可以按照下面的例子来实现
list_empty = [None]*10
1、列表推导式书写形式
[表达式 for 变量 in 列表]
或者
[表达式 for 变量 in 列表 if 条件]
通过 for 语句处理表达式里面的变量,如果还要加条件,就加 if 条件就可以
li = [1, 2, 3, 4, 5, 6, 7, 8, 9]输出
print([x**2 for x in li])
print([x**2 for x in li if x > 5])
print(dict([(x, x*10) for x in li])) #将列表转换为字典
vec = [2, 4, 6]
vec2 = [4, 3, -9]
sq = [vec[i] + vec2[i] for i in range(len(vec))]
print(sq)
print([x*y for x in [1, 2, 3] for y in [1, 2, 3]])
testList = [1, 2, 3, 4]
def mul2(x):
return x*2
print([mul2(i) for i in testList])
[1, 4, 9, 16, 25, 36, 49, 64, 81]
[36, 49, 64, 81]
{1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60, 7: 70, 8: 80, 9: 90}
[6, 7, -3]
[1, 2, 3, 2, 4, 6, 3, 6, 9]
[2, 4, 6, 8]
列表的切片
格式: [start:end:step]
a = [1,2,3,4]输出
b = 'abcdef'
print(a[1:2])
print(b[2:])
print(a[::-1]) # 范围从头到尾,倒序输出
print(b[::-1])
[2]
cdef
[4, 3, 2, 1]
fedcba
python 库里的 reversed() 函数
1 通过列表切片方式复制列表
1.1 列表复制
my_foods = ['pizza', 'falafel', 'caeeot cake']输出
friend_foods = my_foods[:]
print('My favorite foods are:')
print(my_foods)
print('\nMy friend`s favorite foods are:')
print(friend_foods)
My favorite foods are:
['pizza', 'falafel', 'caeeot cake']
My friend`s favorite foods are:
['pizza', 'falafel', 'caeeot cake']
1.2 验证确实实现了两个列表
my_foods.append('cannoli')输出
friend_foods.append('ice cream')
print('My favorite foods are :')
print(my_foods)
print('\nMy friend`s favorite foods are:')
print(friend_foods)
My friend`s favorite foods are:
['pizza', 'falafel', 'caeeot cake']
My favorite foods are :
['pizza', 'falafel', 'caeeot cake', 'cannoli']
My friend`s favorite foods are:
['pizza', 'falafel', 'caeeot cake', 'ice cream']
2 通过简单赋值方式复制列表
my_foods = ['pizza', 'falafel', 'caeeot cake']输出
friend_foods = my_foods
my_foods.append('cannoli')
friend_foods.append('ice cream')
print('My favorite foods are :')
print(my_foods)
print('\nMy friend`s favorite foods are:')
print(friend_foods)
My favorite foods are :
['pizza', 'falafel', 'caeeot cake', 'cannoli', 'ice cream']
My friend`s favorite foods are:
['pizza', 'falafel', 'caeeot cake', 'cannoli', 'ice cream']
Python 列表是链式存储结构,并非顺序存储
Python 列表函数&方法的对象不仅可以是字符串也可以是列表
python 在操作对象的时候,会根据自身对对象的定义进行操作