《Python 简明教程》读书笔记系列四 —— 数据结构

数据结构基本上是这样的 - 它们是能够将一些数据组合在一起的一种结构。换句话说,它们是被用来存储相关数据的集合。

Python 中有四种内置的数据结构 - list, tuple, dictionary and set 。我们将看到如何使用它们以及它们如何使我们的生活变得更简洁。

List

list 是一种保存有序项集合的数据结构。也就是说,你可以在列表中存储一系列项。在 Python 中你使用逗号隔开它们。

项目列表应该使用方括号括起来,以便 Python 能够理解你正在定义一个列表。一旦创建了列表,你就可以在列表中增加、删除或者搜索列表中的项。正因为我们可以增加和删除项,所以我们称列表是一种 可变 数据类型,也就是说这个类型可以被改变。

对象和类的简介

列表是使用对象和类的一个例子。当我们设置一个变量 i 并为它赋值时,例如将整数 5 赋给它。我们可以将其看作是创建一个 对象 i (即,实例)的过程,它对应的 (即,类型)为 int 。实际上,你可以通过查看 help(int) 来更好地理解这一点。

一个类也可以有 方法 ,即只能被该类调用的函数。只有当你拥有该类的对象时,才能使用这些函数。例如,Python 为 列表 类提供了一个 append 函数,它允许你在列表的末尾添加一个元素(或者项)。

一个类也可以有 字段 ,它们只是为该类定义的变量。只有当你拥有该类的对象时,才可以使用这些变量 / 名称。字段也可以用点访问。

示例:

# 这是我的读书清单
booklist = ['b', 'a', 'c']

print('I have', len(booklist), 'books to read.')

print('These books are:', end=' ')
for book in booklist:
    print(book, end=' ')

print('\nI also have d to read.')
booklist.append('d')
print('My booklist list is now', booklist)

print('I will sort my list now')
booklist.sort()
print('Sorted booklist is', booklist)

print('The first book I will read is', booklist[0])
oldbook = booklist[0]
del booklist[0]
print('I read the', oldbook)
print('My booklist is now', booklist)

输出为:

I have 3 books to read.
These books are: b a c 
I also have d to read.
My booklist list is now ['b', 'a', 'c', 'd']
I will sort my list now
Sorted booklist is ['a', 'b', 'c', 'd']
The first book I will read is a
I read the a
My booklist is now ['b', 'c', 'd']

元组

元组用于将多个对象组合在一起。可以将它们近似看作列表,但是没有列表类提供的许多功能。元组的一个重要特征是,它们和字符串一样是 不可变 的,即你不能修改元组。

元组是由一些特殊的项定义的,这些项在一对可选的圆括号中,由逗号隔开。

元组通常用于语句或者用户自定义的函数可以安全地认为值的集合(即,值的元组)不会改变的情况。

例子:

# 尽管圆括号是可选的,
# 我还是建议使用圆括号,
# 来表示元组的开始和结束。
# 因为显式总比隐式更好。
house = ('I', 'You', 'He')
print('Number of people in the house is', len(house))

new_house = 'Her', house # parentheses(圆括号) not required but are a good idea
print('Number of beds in the new house is', len(new_house)-1+len(new_house[1]))
print('All people in new house are', new_house)
print('People brought from old house are', new_house[1])
print('Last person brought from old house is', new_house[1][2])
print('Number of people in the new house is',
      len(new_house)-1+len(new_house[1]))

输出为:

Number of people in the house is 3
Number of beds in the new house is 4
All people in new house are ('Her', ('I', 'You', 'He'))
People brought from old house are ('I', 'You', 'He')
Last person brought from old house is He
Number of people in the new house is 4

字典

我们将 相关联。注意,键必须是唯一的!

注意,对于字典的键,你只能使用不可变对象(比如字符串),但是对于字典的值,不可变对象或者可变对象都可以使用。这基本上就意味着,对于字典的键,你最好只使用简单点的对象。

我们通过 d = {key1 : value1, key2 : value2 },就可以指定字典中的键值对。注意,一个键值对中的键与值由冒号隔开,而不同键值对之间是由逗号隔开,所有的键值对以及冒号、逗号都包含在一对花括号中。

记住,字典中的键值对不以任何方式排序(不像列表中的项一样有从小到大递增的索引)。如果你想要得到一个特殊的顺序。那么在使用字典之前,你必须自己对其进行排序。

你将要使用的字典是 dict 类的一个实例 / 对象。

示例:

# 'sg' 是 's'tudent'g'rades 的缩写,意思是学生成绩

sg = {
    'a': 59,
    'b': 61,
    'c': 65
}

print("a's grade is", sg['a'])

# 删除一个键值对
del sg['c']

print('\nThere are {} students in the student-'
'grade\n'.format(len(sg)))

for name, grade in sg.items():
    print('Student {} at {}'.format(name, grade))

# 添加一个键值对
sg['d'] = 99

if 'd' in sg:
    print("\nd's grade is", sg['d'])

输出为:

a's grade is 59

There are 2 students in the student-grade

Student a at 59
Student b at 61

d's grade is 99

序列

序列的主要特征是:成员测试和索引。这两种操作可以让我们直接从序列中提取特定的部分。

示例:

# 切片
booklist = ['a', 'b', 'c', 'd']
name = 'food'

# 字符串索引
print('Item 0 is', booklist[0])
print('Item 1 is', booklist[1])
print('Item 2 is', booklist[2])
print('Item 3 is', booklist[3])
print('Item -1 is', booklist[-1])
print('Item -2 is', booklist[-2])
print('Character 0 is', name[0])

# 列表切片
print('Item 1 to 3 is', booklist[1:3])
print('Item 2 to end is', booklist[2:])
print('Item 1 to -1 is', booklist[1:-1])
print('Item start to end is', booklist[:])

# 字符串切片
print('characters 1 to 3 is', name[1:3])
print('characters 2 to end is', name[2:])
print('characters 1 to -1 is', name[1:-1])
print('characters start to end is', name[:])

输出:

Item 0 is a
Item 1 is b
Item 2 is c
Item 3 is d
Item -1 is d
Item -2 is c
Character 0 is f
Item 1 to 3 is ['b', 'c']
Item 2 to end is ['c', 'd']
Item 1 to -1 is ['b', 'c']
Item start to end is ['a', 'b', 'c', 'd']
characters 1 to 3 is oo
characters 2 to end is od
characters 1 to -1 is oo
characters start to end is food

集合

集合是简单对象的无序的集合。当对象在集合中的存在比对象在集合中的顺序或者比对象在集合中出现的次数更为重要时,我们就会用到集合。

可以使用集合来测试成员资格,看它是否是另一个集合的子集,找到两个集合之间的交集,等等。

>>> bri = set(['brazil', 'russia', 'india'])
>>> 'india' in bri
True
>>> 'usa' in bri
False
>>> bric = bri.copy()
>>> bric.add('china')
>>> bric.issuperset(bri)
True
>>> bri.remove('russia')
>>> bri & bric # 或者是 bri.intersection(bric)
{'brazil', 'india'}

引用

当你创建了一个对象,并把它赋值给一个变量时,这个变量只是引用了这个对象,变量并不能代表对象自身。因此,你可以把变量名当作一个指针,它指向储存对象的那一块计算机内存。这称作绑定名称到对象。

示例:

# 引用
print('Simple Assignment')
booklist = ['a', 'b', 'c', 'd']
# mylist 只是指向同一个对象的另一个别名
mylist = booklist

# 我看完了第一本书,所以把它从列表中移除
del booklist[0]

print('booklist is', booklist)
print('mylist is', mylist)
# 注意到 booklist 和 mylist 产生了同样的输出
# 输出的都是没有 'apple' 的相同列表
# 这验证了它们都指向着同一个对象

print('Copy by making a full slice')
# 通过全切片来获得一个副本
mylist = booklist[:]
# 移除第一个元素
del mylist[0]

print('booklist is', booklist)
print('mylist is', mylist)
# 现在注意到这两个列表有差异了

输出:

Simple Assignment
booklist is ['b', 'c', 'd']
mylist is ['b', 'c', 'd']
Copy by making a full slice
booklist is ['b', 'c', 'd']
mylist is ['c', 'd']

记住:如果你想要获得列表、或者类似的序列、或更复杂对象的副本,只要不是像整数一样简单的对象,你都需要通过切片操作来获得它的副本。如果你直接把一个变量名赋值给另一个,它们两个都会引用同一个对象。在赋值时你需要注意这一点,不然可能会造成意想不到的结果,从而带来麻烦。

更多的字符串操作

对于字符串我们了解多少呢?

字符串也是一个对象,它有能做很多事的方法,从检验一部分字符串到去除空格。事实上你已经学过一种字符串方法了 —— format 方法。

你在程序中使用的字符串都是 str 类的对象。示例中有一些常用的 str 类的方法。完整的方法列表请在 python 的交互式命令行中输入 help(str)

示例:

# 字符串操作
# 这一个字符串对象
name = 'burgess'

if name.startswith('bur'):
    print('Yes, the string starts with "Bur"')

if 'r' in name:
    print('Yes, it contains the string "r"')

if name.find('urg') != -1:
    print('Yes, it contains the string "war"')

delimiter = '_*_'
mylist = ['a', 'b', 'c', 'd']
print(delimiter.join(mylist))

示例:

Yes, the string starts with "Bur"
Yes, it contains the string "r"
Yes, it contains the string "war"
a_*_b_*_c_*_d

这里我们见识了很多字符串方法的应用。 startswith 方法用于检测字符串是否以给定的字符串开头。 in 方法用于测试给定的字符是否是另一个字符串的一部分。

find 方法用于确定给定子字符串在源字符串中的位置, find 返回 -1 说明找不到给定的字符串。 str 还有一个 join 方法,用于将源字符串按给定的字符分隔,并返回一个大字符串。


参考链接:
数据结构

本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!