Python学习笔记02——内置序列及其操作(数据结构)

1常用的数据结构及其特点

数据结构 特点
元组 静态数据结构,只能查询,无法修改、增加,删除只能删除整个元组
列表 有顺序的数据结构,类似于数组,增删改查都可以操作
字典 储存键值对的数据结构,无序性
Set(集合) 无序性数据结构,且集合中的元素具有唯一性

2元组及其操作

2.1元组的创建

y1=(1,2,3,4)

2.2元组的查询操作

①根据索引查询(超界会报错)

>>> y1=(1,2,3,4)
>>> y1[0]
1
>>> y1[-1]
4
>>> y1[10]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range

②索引查询(不存在会报错)

>>> y1.index(2)
1
>>> y1.index(10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: tuple.index(x): x not in tuple

③存在性查询

>>> 1 in y1
True
>>> 10 in y1
False

④遍历查询

for i in range(len(y1)):
    print(y1[i])
...
1
2
3
4

2.3元组的删除操作

①元组不能删除元素,只能整体删除

>>> del y1
>>> y1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'y1' is not defined

2.4元组的其他操作

①切片 (注意切片、=、copy的异同点)
[::]三个参数分别为起始索引、终止索引、步长
1.起始,终止为左闭右开区间
2.步长可为负值,不可为0

>>> y1=(1,2,3,4)
>>> y1[::-1]
(4, 3, 2, 1)

②求长度、最大值、最小值、求和

>>> len(y1)#求长度
4
>>> max(y1)#最大值
4
>>> min(y1)#最小值
1
>>> sum(y1)#求和
10

3列表及其操作

3.1列表的创建

>>> x1=[1,2,3,4,5]

3.2列表的查询

①根据索引查询(超界会报错)

>>> x1[0]
1
>>> x1[-1]
5
>>> x1[10]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

②索引查询(不存在会报错)(多个只查询第一个索引)

>>> x1.index(1)
0
>>> x1.index(10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 10 is not in list

③存在性查询

>>> x1
[1, 2, 3, 4, 5]
>>> 1 in x1
True
>>> 6 in x1
False

④遍历查询

 for i in range(len(x1)):
     print(x1[i])
...
1
2
3
4
5

3.3列表的修改

①根据索引赋值修改(超界报错)

>>> x1[4]=6
>>> x1
[1, 2, 3, 4, 6]
>>> x1[4]=5
>>> x1
[1, 2, 3, 4, 5]

3.4列表的增加

①增加单个元素append

>>> x1.append(6)
>>> x1
[1, 2, 3, 4, 5, 6]
>>> x1.append([7,8])
>>> x1
[1, 2, 3, 4, 5, 6, [7, 8]]#增加一个列表也不会报错,但是列表会作为一个元素加到最后面

②指定位置插入元素
insert(插入索引,插入元素)

>>> x1
[1, 2, 3, 4, 5, 6]
>>> x1.insert(0,0)
>>> x1
[0, 1, 2, 3, 4, 5, 6]

③增加一个列表extend

>>> x1
[0, 1, 2, 3, 4, 5, 6]
>>> x1.extend([7,8,9])
>>> x1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

3.5列表的删除

①删除指定索引
.pop(索引值) 默认删除最后一个,插入参数可删除指定索引的元素 ,返回该元素
del 列表[索引值]

>>> x1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> x1.pop()
9
>>> x1
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> x1.pop(-1)
8
>>> x1
[0, 1, 2, 3, 4, 5, 6, 7]
>>> del x1[-1]
>>> x1
[0, 1, 2, 3, 4, 5, 6]

②删除指定元素 (删除为第一个指定元素,多个重复只删除第一个)

>>> x1=[1,2,3,2,4,2]
>>> x1
[1, 2, 3, 2, 4, 2]
>>> x1.remove(2)
>>> x1
[1, 3, 2, 4, 2]

③删除列表本身

>>> del x1

3.6列表的排序操作

①将原列表颠倒
[::-1]是切片出一个新的列表与原列表倒序
reverse()是将元列表本身进行倒叙操作,且没有返回值

>>> x1=[1,2,3,4,1]
>>> x1[::-1]
[1, 4, 3, 2, 1]
>>> x1
[1, 2, 3, 4, 1]
>>> x1.reverse()
>>> x1
[1, 4, 3, 2, 1]

②将原列表按顺序(可自定义)排序

>>> x1
[1, 4, 3, 2, 1]
>>> x1.sort()
>>> x1
[1, 1, 2, 3, 4]

3.7列表的其它操作

①求长度、最大值、最小值、求和、统计次数

>>> x1
[1, 1, 2, 3, 4]
>>> len(x1)
5
>>> max(x1)
4
>>> min(x1)
1
>>> sum(x1)
11
>>> x1.count(1)
2

4字典及其操作

4.1字典的创建

①直接创建

 english={"we":"我们","world":"世界","company":"公司"}
>>> english
{'we': '我们', 'world': '世界', 'company': '公司'}

②通过一个序列创建(列表、元组、SET)
dict.fromkeys(参数1,参数2) 参数1为键值序列对象,参数2为默认值,可写可不写

>>> shunxu=dict.fromkeys(x1,"默认值")
>>> shunxu
{1: '默认值', 2: '默认值', 3: '默认值', 4: '默认值', 5: '默认值'}

4.2字典的查询

①根据关键字查询对应值
1.english[""]直接查询如果没有对应键,则会报错
2.english.get()方式查询,如果没有对应键,则返回空

>>> english["we"]
'我们'
>>> english["city"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'city'
>>> english.get("we")
'我们'
>>> english.get("city")

②查询所有键值

 english.keys()
dict_keys(['we', 'world', 'company'])

③查询所有values值

 english.values()
dict_values(['我们', '世界', '公司'])

④查询所有键值对

english.items()
dict_items([('we', '我们'), ('world', '世界'), ('company', '公司')])

⑤遍历字典
for k,v in english.items()

 for k,v in english.items():    print("键:"+k+"值:"+v)
...
键:we值:我们
键:world值:世界
键:company值:公司

4.3字典的增加/修改

①直接通过赋值增加/修改

>>> english["city"]="家"#不存在该键则为增加
>>> english
{'we': '我们', 'world': '世界', 'company': '公司', 'city': '家'}
>>> english["city"]="城市"#存在对应键则为修改
>>> english
{'we': '我们', 'world': '世界', 'company': '公司', 'city': '城市'}

4.4字典的删除

①删除单个元素和删除字典本身都用del

>>> del english["city"]
>>> english
{'we': '我们', 'world': '世界', 'company': '公司'}
>>> del english
>>> english
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'english' is not defined

4.5字典的其它操作

①清空字典

>>> english
{'we': '我们', 'world': '世界', 'company': '公司'}
>>> english.clear()
>>> english
{}

②字典的复制(与直接赋值的区别)(字典不可切片)

>>> english={"we":"我们","world":"世界","company":"公司"}
>>> english
{'we': '我们', 'world': '世界', 'company': '公司'}
>>> english1=english
>>> english3=english.copy()
>>> english
{'we': '我们', 'world': '世界', 'company': '公司'}
>>> english1
{'we': '我们', 'world': '世界', 'company': '公司'}
>>> english3
{'we': '我们', 'world': '世界', 'company': '公司'}
>>>
>>>
>>> english["we"]="你们"
>>> english
{'we': '你们', 'world': '世界', 'company': '公司'}
>>> english1
{'we': '你们', 'world': '世界', 'company': '公司'}#通过赋值复制的对象随着原对象的改变而改变
>>> english3
{'we': '我们', 'world': '世界', 'company': '公司'}

5集合及其操作

5.1集合的创建

注意空集合不能用{}创建,只能用set()创建

empty=set()
>>> empty
set()
>>> mix={1,2,3,"Hello"}
>>> mix
{1, 2, 3, 'Hello'}

5.2集合的增加和删除

>>> mix.add(4)
>>> mix
{1, 2, 3, 4, 'Hello'}
>>> mix.remove("Hello")
>>> mix
{1, 2, 3, 4}

6列表推导式、字典推导式、集合推导式

形式:
[变量 for 条件]
{k:v for 条件}
{变量 for 条件}

>>> l1=[x for x in range(10) if x%2==0]
>>> l1
[0, 2, 4, 6, 8]
>>> d1={1:1,2:4,3:9,4:16}
>>> d1
{1: 1, 2: 4, 3: 9, 4: 16}
>>> d2={v:k for k,v in d1.items()}
>>> d2
{1: 1, 4: 2, 9: 3, 16: 4}
>>>
>>>
>>> s1={i**2 for i in range(10)}
>>> s1
{0, 1, 64, 4, 36, 9, 16, 49, 81, 25}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章