您的位置:首页 > 理论基础 > 数据结构算法

第一章 python中重要的数据结构(上)

2015-10-03 00:13 369 查看
最近,由于工作需要,使用python开发公司的运维自动化平台,所以找本书来并结合官方手册,开始python的学习之旅。

一、列表

【含义】:列表用中括号表示,通过逗号进行分隔一组数据(可以为不同的数据类型),如以下的声明:

>>> language = ['chinese','english','japanese']
>>> contries = ['China','Amercia','England','Japan']

>>> edward = ['Edward Gumby',42]  #不同的数据类型
>>> john = ['John Smith',50]
>>> database = [edward,john] #列表可以嵌套


【操作】:访问、插入、删除、求最大值最小值及长度等

1、访问:可以通过索引或分片进行列表的遍历访问,索引可以访问某个位置的值,分片可以灵活的访问一定范围的值,如下所示:

>>> language = ['chinese','english','japanese']
# 索引访问
>>> language[0]
'chinese'

# 分片访问
# 第一个索引元素包括在分片中,第二个索引不包括在分片中
# 可以设置分片的步长
>>> language[::]
['chinese', 'english', 'japanese']
>>> language[0:3]
['chinese', 'english', 'japanese']
>>> language[0:2]
['chinese', 'english']
>>> language[1:2]
['english']
# 设置步长为2
>>> num = [1,2,3,4,5,6,7,8,9]
>>> num[0:9:2]
[1, 3, 5, 7, 9]

21  >>> num = [1, 2, 3]
22  >>> max(num)
23  3
24  >>> min(num)
25  1
26  >>> len(num)
27  3


2、修改、插入和删除操作,由此看来列表是可以进行修改的。

#修改列表某个元素:索引赋值
>>> num = [1,2,3]
>>> num[0]=5  #必须为存在的位置索引赋值,否则报错
>>> num
[5, 2, 3]

#修改列表某段范围的值:分片赋值
>>> num[2:]=[6,7,9] #分片赋值元素个数可以不等长
>>> num
[5, 2, 6, 7, 9]

#删除某个元素
>>> del num[4]
>>> num
[5, 2, 6, 7]


【方法】对于列表,python内置了诸多方法供操作,主要常用的有以下几个:

#append方法:用于在列表最后添加元素,该方法直接修改原列表并返回修改完后的新列表
>>> str = ['a','b','c','d']
>>> str.append('e')
>>> str
['a', 'b', 'c', 'd', 'e']

#count方法:统计某个元素在列表中出现的次数
>>> str = ['a','b','c','d']
>>> str.count('a')
1

#extend方法:list.extend(L),L指的是列表对象
#用另一个列表扩展一个列表,相当于两个列表连接,
#但是又不同于连接操作,因为extend方法直接修改原列表并返回,
#连接操作不影响原有的列表值
>>> str1 = ['hello,']
>>> str2 = ['world']
>>> str1.extend(str2)
>>> str1
['hello,', 'world']

>>> str3 = ['a']
>>> str4 = ['5']
>>> str3 = str3+str4 #效率没有extend高
>>> str3
['a', '5']

#index方法:返回匹配项的第一个索引位置
>>> knight = ['we','you','we','me','he']
>>> knight.index('we')
0

#insert方法:list.insert(i, x)插入x到该i位置
>>> knight.insert(1,'she')
>>> knight
['we', 'she', 'you', 'we', 'me', 'he']

#remove方法:list.remove(x)
#删除列表中为X的第一个出现元素
>>> knight = ['we', 'she', 'you', 'we', 'me', 'he']
>>> knight.remove('we')
>>> knight
['she', 'you', 'we', 'me', 'he']

#reverse方法:将元素倒序存储
>>> str = ['a', 'b']
>>> str.reverse()
>>> str
['b', 'a']

#sort方法:list.sort(key=None, reverse=False)
#直接改变原列表顺序,而不是改变副本,对于这种需求如
#仅仅对副本进行排序,不改变原列表不能直接用sort
>>> num = [1,3,2,4]
>>> num.sort()
>>> num
[1, 2, 3, 4]

#pop方法:list.pop([i])
#删除指定索引位置的元素值,并返回该值
>>> num = [1, 2, 3, 4]
>>> num.pop(3)
4


【常用举例】:模拟实现堆栈操作和队列操作

堆栈:后进先出

>>> stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
7
>>> stack
[3, 4, 5, 6]
>>> stack.pop()
6
>>> stack.pop()
5
>>> stack
[3, 4]


队列:先进先出

>>> from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])
>>> queue.append("Terry")           # Terry arrives
>>> queue.append("Graham")          # Graham arrives
>>> queue.popleft()                 # The first to arrive now leaves
'Eric'
>>> queue.popleft()                 # The second to arrive now leaves
'John'
>>> queue                           # Remaining queue in order of arrival
deque(['Michael', 'Terry', 'Graham'])
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: