目录
2.1 序列概述
数据结构是以某种方式(如通过编号)组合起来的数据元素(如数、字符乃至其他数据结构)集合。在Python中,最基本的数据结构为序列(sequence)。
序列中的每个元素都有编号,即其位置或索引,其中第一个元素的索引为0,第二个元素的索引为1,依此类推。
元组是一种特殊的序列,类似于列表,只是不能修改。
列表和元组的主要不同在于,列表是可以修改的,而元组不可以。
列表适用于需要中途添加元素的情形,而元组适用于出于某种考虑需要禁止修改序列的情形。
Python支持一种数据结构的基本概念,名为容器(container)。容器基本上就是可包含其他对象的对象。两种主要的容器是序列(如列表和元组)和映射(如字典)。
在序列中,每个元素都有编号,而在映射中,每个元素都有名称(也叫键)。
2.2 通用的序列操作
有几种操作适用于所有序列,包括索引、切片、相加、相乘和成员资格检查。另外,Python还提供了一些内置函数,可用于确定序列的长度以及找出序列中最大和最小的元素。
注意 有一个重要的操作,它就是迭代(iteration)。对序列进行迭代意味着对其每个元素都执行特定的操作。
2.2.1 索引
序列中的所有元素都有编号——从0开始递增。
字符串就是由字符组成的序列。索引0指向第一个元素,不同于其他一些语言,Python没有专门用于表示字符的类型,因此一个字符就是只包含一个元素的字符串。
>>> greeting='Hello'
>>> greeting[0]
'H'
>>> greeting="Hello"
>>> greetint[o]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'greetint' is not defined
使用索引来获取元素。这种索引方式适用于所有序列。当使用负数索引时,Python将从右(即从最后一个元素)开始往左数,因此-1是最后一个元素的位置。
>>> Greeting='Hello'
>>> Greeting[0]
'H'
>>> Greeting[-1]
'o'
>>> Greeting[-2]
'l'
>>> Greeting[-4]
'e'
>>> Greeting[10]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> fourth=input('Year: ')[3]
Year: 2019
>>> fourth
'9'
# 将以数指定年、月、日的日期打印出来
months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
]
# 一个列表,其中包含数1~31对应的结尾
endings = ['st', 'nd', 'rd'] + 17 * ['th'] \
+ ['st', 'nd', 'rd'] + 7 * ['th'] \
+ ['st']
year = input('Year: ')
month = input('Month (1-12): ')
day = input('Day (1-31): ')
month_number = int(month)
day_number = int(day)
# 别忘了将表示月和日的数减1,这样才能得到正确的索引
month_name = months[month_number-1]
ordinal = day + endings[day_number-1]
print(month_name + ' ' + ordinal + ', ' + year)
=============== RESTART: C:/Users/qidia/Desktop/2.2_通用的序列操作.py ===============
Year: 2019
Month (1-12): 7
Day (1-31): 20
July 20th, 2019
=============== RESTART: C:/Users/qidia/Desktop/2.2_通用的序列操作.py ===============
Year: 2019
Month (1-12): 7
Day (1-31): 21
July 21st, 2019
=============== RESTART: C:/Users/qidia/Desktop/2.2_通用的序列操作.py ===============
Year: 2019
Month (1-12): 7
Day (1-31): 22
July 22nd, 2019
2.2.2 切片
使用切片(slicing)来访问特定范围内的元素。
>>> tag = '<a href="http://www.python.org">Python web site</a>'
>>> tag[9:30]
'http://www.python.org'
>>> tag[32:-4]
'Python web site'
注意:切片适用于提取序列的一部分,其中的编号非常重要:第一个索引是包含的第一个元素的编号,但第二个索引是切片后余下的第一个元素的编号。
>>> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> numbers[2:4]
[3, 4]
>>> numbers[0:1]
[1]
# 从类似于http://www.something.com的URL中提取域名
url = input('Please enter the URL:')
domain = url[11:-4]
print("Domain name: " + domain)
Please enter the URL:http://www.python.org
Domain name: python
执行切片操作时,你显式或隐式地指定起点和终点,但通常省略另一个参数,即步长。在普通切片中,步长为1。这意味着从一个元素移到下一个元素,因此切片包含起点和终点之间的所有元素。
步长不能为0,否则无法向前移动,但可以为负数,即从右向左提取元素。
>>> Numbers=[1,2,3,4,5,6,7,8,9,10]
>>> Numbers[0:10]#默认步长为1,从左往右
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> Numbers[0:10:1]#步长为1,从左往右
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> Numbers[0:10:2]#步长为2,从左往右
[1, 3, 5, 7, 9]
>>> Numbers[0:10:3]#步长为3,从左往右
[1, 4, 7, 10]
>>> Numbers[0:10:4]#步长为4,从左往右
[1, 5, 9]
>>> Numbers[8:3:-2]#步长为2,从右往左
[9, 7, 5]
2.2.3 相加
使用加法运算符来拼接序列
>>> [1,2,3]+[4,5,6]
[1, 2, 3, 4, 5, 6]
>>> 'Hello'+' World'
'Hello World'
>>> "Hello"+" World"
'Hello World'
>>> [1,2,3]+"World"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "str") to list
注意:不能拼接列表和字符串,虽然它们都是序列。一般而言,不能拼接不同类型的序列。
2.2.4 相乘
将序列与数x相乘时,将重复这个序列x次来创建一个新序列。
>>> 'Python '*5
'Python Python Python Python Python '
>>> "Python "*5
'Python Python Python Python Python'
>>> [42]*5
[42, 42, 42, 42, 42]
>>> [Python]*5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
#创造长度为5的空列表
>>> sequence=[None]*5
>>> sequence[0:5]
[None, None, None, None, None]
>>>
# 在位于屏幕中央且宽度合适的方框内打印一个句子
sentence = input("Sentence: ")
screen_width = 80
text_width = len(sentence)
box_width = text_width + 6
left_margin = (screen_width - box_width) // 2
print()
print(' ' * left_margin + '+' + '-' * (box_width-4) + '+')
print(' ' * left_margin + '| ' + ' ' * text_width + ' |')
print(' ' * left_margin + '| ' + sentence + ' |')
print(' ' * left_margin + '| ' + ' ' * text_width + ' |')
print(' ' * left_margin + '+' + '-' * (box_width-4) + '+')
print()
Sentence: http://www.python.org
+-----------------------+
| |
| http://www.python.org |
| |
+-----------------------+
2.2.5 成员资格
要检查特定的值是否包含在序列中,可使用运算符in。
它检查是否满足指定的条件,并返回相应的值:满足时返回True,不满足时返回False。
这样的运算符称为布尔运算符,而前述真值称为布尔值。
>>> permission ='rw'
>>> 'w' in permission
True
>>> 'x' in permission
False
在较早的Python版本中,只能对字符串执行这种成员资格检查——确定指定的字符是否包含在字符串中,但现在可使用运算符in来检查指定的字符串是否为另一个字符串的子串。
# 检查用户名和PIN码
database = [
['albert', '1234'],
['dilbert', '4242'],
['smith', '7524'],
['jones', '9843']
]
username = input('User name: ')
pin = input('PIN code: ')
if [username, pin] in database: print('Access granted')
else: print("Error,Please try again.")
=============== RESTART: C:/Users/qidia/Desktop/2.2_通用的序列操作.py ===============
User name: smith
PIN code: 7524
Access granted
=============== RESTART: C:/Users/qidia/Desktop/2.2_通用的序列操作.py ===============
User name: tony
PIN code: 1234
Error,Please try again.
常用函数:长度(len)、最小值(min)、最大值(max)
>>> numbers=[1,2,3,4,5,6,7,8,9,10]
>>> len(numbers)
10
>>> min(numbers)
1
>>> max(numbers)
10
>>> string=[tony,John,Smith]
2.3 列表:Python的主力
2.3.1 函数list
list 将序列转为列表
可将任何序列(而不仅仅是字符串)作为list的参数
>>> x='hello'
>>> list(x)
['h', 'e', 'l', 'l', 'o']
>>> y="Hello"
>>> list(y)
['H', 'e', 'l', 'l', 'o']
>>> z=[1,2,3,4,5]
>>> list(z)
[1, 2, 3, 4, 5]
2.3.2 基本的列表操作
(1)修改列表:给元素赋值
修改列表很容易,只需使用第1章介绍的普通赋值语句即可,但不是使用类似于x = 2这样的赋值语句,而是使用索引表示法给特定位置的元素赋值。
>>> Num=[1,2,3]
>>> Num[1]=8
>>> Num
[1, 8, 3]
>>> Num[5]=9
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
注意:不能给不存在的元素赋值。
(2)删除元素
从列表中删除元素也很容易,只需使用del语句。
注意到Dee-Dee彻底消失了,而列表的长度也从5变成了4。除用于删除列表元素外,del语句还可用于删除其他东西。
>>> Names=['Alice', 'Beth', 'Cecil', 'Dee-Dee', 'Earl']
>>> Names
['Alice', 'Beth', 'Cecil', 'Dee-Dee', 'Earl']
>>> del Names[3]
>>> Names
['Alice', 'Beth', 'Cecil', 'Earl']
(3)给切片赋值
通过使用切片赋值,可将切片替换为长度与其不同的序列
使用切片赋值还可在不替换原有元素的情况下插入新元素。
>>> name = list('Perl')
>>> name
['P', 'e', 'r', 'l']
>>> name[2:] = list('ar')
>>> name
['P', 'e', 'a', 'r']
2.3.3 列表方法
(1)append 添加到列表末尾
方法append用于将一个对象附加到列表末尾
>>> Num=[1,2,3]
>>> Num.append(4)
>>> Num
[1, 2, 3, 4]
(2)clear 清空列表内容
方法clear就地清空列表的内容。
>>> Num=[1,2,3,4,5]
>>> Num
[1, 2, 3, 4, 5]
>>> Num.clear()
>>> Num
[]
(3)copy 复制
方法copy复制列表。
>>> Numa=[1,3,5,7,9]
>>> Num1=[1,3,5,7,9]
>>> Num2=Num1
>>> Num2
[1, 3, 5, 7, 9]
>>> Num3=Num1.copy()
>>> Num3
[1, 3, 5, 7, 9]
>>> Num3[1]=6
>>> Num3
[1, 6, 5, 7, 9]
>>> Num1
[1, 3, 5, 7, 9]
(4)count 统计出现次数
方法count计算指定的元素在列表中出现了多少次。
>>> String=['to', 'be', 'or', 'not', 'to', 'be']
>>> String
['to', 'be', 'or', 'not', 'to', 'be']
>>> String.count('to')
2
>>>
>>> Num=[[1, 2], 1, 1, [2, 1, [1, 2]]]
>>> Num
[[1, 2], 1, 1, [2, 1, [1, 2]]]
>>> Num.count(1)
2
(5)extend 附加
方法extend让你能够同时将多个值附加到列表末尾,为此可将这些值组成的序列作为参数提供给方法extend。
换而言之,你可使用一个列表来扩展另一个列表。
>>> a=[1,2,3]
>>> b=[4,5,6]
>>> a.extend(b)
>>> a
[1, 2, 3, 4, 5, 6]
>>> b
[4, 5, 6]
(6)index 首次出现的位置
方法index在列表中查找指定值第一次出现的索引。
>>> String=['I','am','learn','English','every','day']
>>> String
['I', 'am', 'learn', 'English', 'every', 'day']
>>>
>>> String.index('learn')
2
>>> String.index('are')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 'are' is not in list
(7)insert 插入
方法insert用于将一个对象插入列表。
>>> Num=[1,2,3,5,6]
>>> Num
[1, 2, 3, 5, 6]
>>> Num.insert(3,'Four')
>>> Num
[1, 2, 3, 'Four', 5, 6]
(8)pop
方法pop从列表中删除一个元素(末尾为最后一个元素),并返回这一元素。
>>> Num=[1,2,3,4,5,6]
>>> Num.pop()
6
>>> Num
[1, 2, 3, 4, 5]
>>>
>>> Num.pop(0)
1
>>> Num
[2, 3, 4, 5]
(9)remove
方法remove用于删除第一个为指定值的元素。
>>> String=['to','be','or','not','to','be']
>>> String.remove('to')
>>> String
['be', 'or', 'not', 'to', 'be']
(10)reverse
方法reverse按相反的顺序排列列表中的元素
>>> Num=[1,2,3,4,5]
>>> Num
[1, 2, 3, 4, 5]
>>> Num.reverse()
>>> Num
[5, 4, 3, 2, 1]
(11)sort
方法sort用于对列表就地排序.
对原来的列表进行修改,使其元素按顺序排列,而不是返回排序后的列表的副本。
>>> Num=[1,8,3,6,5,4]
>>> Num
[1, 8, 3, 6, 5, 4]
>>> Num.sort()
>>> Num
[1, 3, 4, 5, 6, 8]
(12)高级排序
方法sort接受两个可选参数:key和reverse。这两个参数通常是按名称指定的,称为关键字参数.
>>> String=['aardvark', 'abalone', 'acme', 'add', 'aerate']
>>> String
['aardvark', 'abalone', 'acme', 'add', 'aerate']
>>> String.sort(key=len)
>>> String
['add', 'acme', 'aerate', 'abalone', 'aardvark']
>>> Num=[1,8,3,6,5,4]
>>> Num
[1, 8, 3, 6, 5, 4]
>>> Num.sort()
>>> Num
[1, 3, 4, 5, 6, 8]
>>> Num.sort(reverse=True)
>>> Num
[8, 6, 5, 4, 3, 1]
2.4 元组:不可修改的序列
与列表一样,元组也是序列,唯一的差别在于元组是不能修改的(你可能注意到了,字符串也不能修改)。
函数tuple的工作原理与list很像:它将一个序列作为参数,并将其转换为元组5。如果参数已经是元组,就原封不动地返回它。
2.5 小结
2.5.1 相关概念
- 序列:序列是一种数据结构,其中的元素带编号(编号从0开始)。列表、字符串和元组都属于序列,其中列表是可变的(你可修改其内容),而元组和字符串是不可变的(一旦创建,内容就是固定的)。要访问序列的一部分,可使用切片操作:提供两个指定切片起始和结束位置的索引。要修改列表,可给其元素赋值,也可使用赋值语句给切片赋值。
- 成员资格:要确定特定的值是否包含在序列(或其他容器)中,可使用运算符in。将运算符in用于字符串时情况比较特殊——这样可查找子串。
- 方法:一些内置类型(如列表和字符串,但不包括元组)提供了很多有用的方法。方法有点像函数,只是与特定的值相关联。方法是面向对象编程的一个重要方面,这将在第7章介绍。
2.5.2 几个函数
序号 | 函数 | 描述 |
1 | len(seq) | 返回序列的长度 |
2 | list(seq) | 将序列转换为列表 |
3 | max(args) | 返回序列或一组参数中的最大值 |
4 | min(args) | 返回序列和一组参数中的最小值 |
5 | reversed(seq) | 让你能够反向迭代序列 |
6 | sorted(seq) | 返回一个有序列表,其中包含指定序列中的所有元素 |
7 | tuple(seq) | 将序列转换为元组 |