美文网首页
Python -- 数据结构

Python -- 数据结构

作者: liaozb1996 | 来源:发表于2018-03-09 08:41 被阅读0次

使用 list 实现栈(后进先出)

  • list.append() -- 入栈
  • list.pop() -- 出栈

队列

使用 list 并不能高效地实现队列(先进先出),因为出列后还要依次移动后面元素;
使用 collections.deque 来实现队列,可以快速地从两端插入或移除元素

>>> 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'])

List Comprehensions

List Comprehensions 用于快速地生成列表: [表达式:for...if...] (可使用任意多个for... , if...)

>>> square = [i * i for i in range(5) if i != 3]
>>> square
[0, 1, 4, 16]

del 语句

del 可用于根据索引来删除列表中的元素;
对比:

  • list.remove() 根据值来删除元素
  • list.pop() 根据索引删除元素,并返回元素的值
>>> numbers = [1, 2, 3, 4, 5]
>>>
>>> del numbers[2]
>>> numbers
[1, 2, 4, 5]
>>>
>>> del numbers[1:3]
>>> numbers
[1, 5]
>>>
>>> del numbers  # 再次访问 number 将抛出错误

Tuple 元组

元组一种不可变类型的序列

>>> t1 = 1,2,3  # 创建元组
>>> t1
(1, 2, 3)
>>>
>>> t2 = (1, 2, 3) # 创建元组
>>> t2
(1, 2, 3)
>>>
>>> t2[1]
2
>>> t2[1] = 4  # 元组是不可变类型,所以不能对其元素进行赋值
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

创建元组:

  • 创建空元组 t = ()
  • 创建仅包含一个元素的元组 t = (1, ) [ 注意逗号 ]
x, y, z = 1, 2, 3  # 右边的数字先打包成元组,再解包赋值到右边

Set 集合

set 是一种有序的序列,其包含的元素都是唯一的

>>> s1 = {1, 2, 3 , 3, 4}  # 创建集合
>>>
>>> s2 = set([1, 2, 3, 3, 4])  # 创建集合
>>>
>>> s1
set([1, 2, 3, 4])
>>> s2
set([1, 2, 3, 4])
>>>
>>> 1 in s1  # 判断元素是否在集合内
True
>>>
>>> emptySet = set()  # 创建空集只能使用函数
>>> dictionary = {}  # 这是创建空字典,而表示空集

set 支持数学的集合运算:

  • a - b # letters in a but not in b
  • a | b # letters in a or b or both 【并集】
  • a & b # letters in both a and b 【交集】
  • a ^ b # letters in a or b but not both 【补集】

Dictionary 字典

  • 字典是无序的
  • 字典的 key 可以是任何不可变类型,一般为字符串
>>> people = {'Tom': 18, 'John': 19, 'Amy': 20}
>>> people
{'Amy': 20, 'John': 19, 'Tom': 18}

dict() 创建字典

方式一:包含键值对的列表

>>> dict([('Tom', 20), ('John', 19), ('Amy', 18)])
{'Amy': 18, 'John': 19, 'Tom': 20}

方式二:如果 key 是字符串,可以使用关键字参数创建字典

>>> dict(Tom = 20, John = 18, Amy = 19)
{'Amy': 19, 'John': 18, 'Tom': 20}

del 删除成员:

>>> del people['Tom']
>>> people
{'Amy': 20, 'John': 19}

in 判断成员是否在字典里

>>> 'John' in people
True

字典for

直接遍历是返回字典的 key

>>> for key in people:
...     print(key)
...
Amy
John

使用 dict.items() 返回键值对

>>> for k, v in people.items():
...     print(k, v)
...
('Amy', 20)
('John', 19)

循环技巧

  • 使用 dict.items() 可以同时遍历字典的键值对
for k, v in dict.items():
    pass    
  • 使用 enumerate() 可以同时获得一个序列的 位置索引 和 值
>>> for i, v in enumerate(people):
...     print(i, v)
...
(0, 'John')
(1, 'Tom')
(2, 'Amy')
  • 使用 zip() 同时遍历多个序列
>>> works = ('student', 'teacher', 'engineer')
>>>
>>> for name, work in zip(people, works):
...     print(name, work)
...
('John', 'student')
('Tom', 'teacher')
('Amy', 'engineer')
  • 使用 reversed() 反向遍历序列
  • 使用 sorted() 遍历已排序的序列
    `

序列比较

相同类型的序列可以用 比较操作符 进行比较;

比较的规则

图片.png

两个元素按顺序比较,直到得出结果;如果两个元素又是同类型序列,则进入元素进行递归比较。如上图,比较到红线时得出结果

>>> list1 = [1, 2, 3, 4]
>>> list2 = [1, 2, 4, 5]
>>> list1 < list2
True

相关文章

网友评论

      本文标题:Python -- 数据结构

      本文链接:https://www.haomeiwen.com/subject/tnosfftx.html