八、序列

  1. 什么是序列(sequence)?

在python中会有很多非常重要却又让人琢磨不透的概念,比如迭代器、生成器等等,今天遇到的序列也是,因此每碰到一个重要的概念我都会彻底的搞定它。什么是序列?我查过很多资料,说法不一,这时候最好的办法就是去查找python官方文档,但是由于python版本不同的原因也会不一样。先来康康python2.7版本:

5.6. Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange
There are seven sequence types: strings, Unicode strings, lists, tuples, bytearrays, buffers, and xrange objects.
Python2.7.18官方文档

这里的7种序列类型中有三种是我们非常熟悉的,lists(列表)、tuples(元组)和strings(字符串),另外4种是unicode字符串、缓冲区对象、字节数组和xrange对象。
到了python3以后,对于序列的定义变得更加的规范化:

Sequence Types — list, tuple, range
There are three basic sequence types: lists, tuples, and range objects. Additional sequence types tailored for processing of binary data and text strings are described in dedicated sections.
Python3.8.6官方文档

基本序列类型有列表、元组和range对象,关于range对象:

Ranges
The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops.

看一些例子就明白了:

>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(1, 11))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> list(range(0, 30, 5))
[0, 5, 10, 15, 20, 25]
>>> list(range(0, 10, 3))
[0, 3, 6, 9]
>>> list(range(0, -10, -1))
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> list(range(0))
[]
>>> list(range(1, 0))
[]

关于The sequence type for the processing of binary data:

Binary Sequence Types — bytes, bytearray, memoryview
The core built-in types for manipulating binary data are bytes and bytearray. They are supported by memoryview which uses the buffer protocol to access the memory of other binary objects without needing to make a copy.
The array module supports efficient storage of basic data types like 32-bit integers and IEEE754 double-precision floating values.

不做深入了解。

关于The sequence type for the processing of text strings:

Text Sequence Type — str
Textual data in Python is handled with str objects, or strings. Strings are immutable sequences of Unicode code points.

没错,这就是我们熟悉的字符串类型。

  1. 序列类型的共同点
  • 都可以通过索引访问
  • 支持切片操作
  • 共用很多的操作符(如重复操作符、成员操作符等)
    等等
  1. 序列类型常用的BIF
  • list(iterable) tuple(iterable) str(object)

直接上栗子:

>>> a = list()
>>> a
[]
>>> b = list((1,2))
>>> b
[1, 2]
>>> c = tuple([1, 2])
>>> c
(1, 2)
>>> d = str([1,2,3,4])
>>> d
'[1, 2, 3, 4]'
  • max() min() sum()
>>> list1 = [1, 34, 56, 22]
>>> max(list1)
56
>>> str1 = "abdca b c"
>>> max(str1)
'd'
>>> max(23, 56, 89, 12)
89
>>> min(list1)
1
>>> sum(list1)
113
>>> sum(list1, 10) # sum有一个可选参数start,表示从该值加起,默认是0
123
  • len()
>>> len(list1)
4
  • enumerate(iterable)
    这是一个很有用的函数,返回一个由二元组构成的可迭代对象。
>>> list2 = ["北京","上海","广州","厦门"]
>>> liter = enumerate(list2)
>>> for  city in liter:
	print(city)

	
(0, '北京')
(1, '上海')
(2, '广州')
(3, '厦门')
  • sorted()
>>> list1
[1, 34, 56, 22]
>>> list2 = list1.copy()
>>> list2
[1, 34, 56, 22]
>>> list1.sort() # sort()方法和sorted()函数实现效果一致,差别在于sort()方法是原地排序(不拷贝),sorted()函数则返回排序后的拷贝值。
>>> list1
[1, 22, 34, 56]
>>> sorted(list2)
[1, 22, 34, 56]
>>> list2
[1, 34, 56, 22]
  • reversed()
>>> list1 = [12,34, 32, 80]
>>> reversed(list1)
<list_reverseiterator object at 0x0000018678147130>
>>> list1.reverse()  # reverse方法和reversed()函数实现效果一致,区别是reverse()原地翻转,reversed()返回的是翻转后的迭代器对象或者reversed对象
>>> list1
[80, 32, 34, 12]
>>> type(list1)
<class 'list'>
>>> str2 = "ajfgierhg"
>>> reversed(str2)
<reversed object at 0x00000186781475B0>
>>> type(reversed(str2))
<class 'reversed'>
>>> tup = (23,5,5,6,33)
>>> reversed(tup)
<reversed object at 0x00000186781475B0>
  • zip()
    zip()函数返回由多重映射元组构成的迭代器对象, 参数为任意多个的可迭代对象。
>>> name = ["Amy", "Bob","Lucy","Tom"]
>>> ID= ("010", "013", "022", "050")
>>> total_score = [123, 140, 68, 50]
>>> iterb = zip(name, ID, total_score)
>>> for item in iterb:
	print(item)

	
('Amy', '010', 123)
('Bob', '013', 140)
('Lucy', '022', 68)
('Tom', '050', 50)
posted @ 2020-11-04 12:37  YuMi-sharing  阅读(306)  评论(0)    收藏  举报