# 2.4 序列

### 序列数据类型

Python 有三种序列数据类型。

• 字符串：如 'Hello'。字符串是字符序列
• 列表：如 [1, 4, 5]
• 元组：如 ('GOOG', 100, 490.1)

a = 'Hello'               # String
b = [1, 4, 5]             # List
c = ('GOOG', 100, 490.1)  # Tuple

# Indexed order
a[0]                      # 'H'
b[-1]                     # 5
c[1]                      # 100

# Length of sequence
len(a)                    # 5
len(b)                    # 3
len(c)                    # 3


>>> a = 'Hello'
>>> a * 3
'HelloHelloHello'
>>> b = [1, 2, 3]
>>> b * 2
[1, 2, 3, 1, 2, 3]
>>>


>>> a = (1, 2, 3)
>>> b = (4, 5)
>>> a + b
(1, 2, 3, 4, 5)
>>>
>>> c = [1, 5]
>>> a + c
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate tuple (not "list") to tuple


### 切片

a = [0,1,2,3,4,5,6,7,8]

a[2:5]    # [2,3,4]
a[-5:]    # [4,5,6,7,8]
a[:3]     # [0,1,2]

• 索引 startend 必须是整数。
• 切片不包括结尾值。这就像数学上的半开区间。
• 如果省略索引，则它们默认为序列的开头或结尾。

### 切片与重新赋值

# Reassignment
a = [0,1,2,3,4,5,6,7,8]
a[2:4] = [10,11,12]       # [0,1,10,11,12,4,5,6,7,8]


# Deletion
a = [0,1,2,3,4,5,6,7,8]
del a[2:4]                # [0,1,4,5,6,7,8]


### 序列的缩减

>>> s = [1, 2, 3, 4]
>>> sum(s)
10
>>> min(s)
1
>>> max(s)
4
>>> t = ['Hello', 'World']
>>> max(t)
'World'
>>>


### 迭代序列

>>> s = [1, 4, 9, 16]
>>> for i in s:
...     print(i)
...
1
4
9
16
>>>


for x in s:         # x is an iteration variable
...statements


### break 语句

for name in namelist:
if name == 'Jake':
break
...
...
statements


break 语句执行时，它退出循环并且进入下一个语句。break 语句仅应用于最内部的循环。如果此循环在另一个循环的内部，那么 break 不会中断外部循环。

### continue 语句

for line in lines:
if line == '\n':    # Skip blank lines
continue
# More statements
...


### 遍历整数

for i in range(100):
# i = 0,1,...,99


range() 函数的语法是range([start,] end [,step])

for i in range(100):
# i = 0,1,...,99
for j in range(10,20):
# j = 10,11,..., 19
for k in range(10,50,2):
# k = 10,12,...,48
# Notice how it counts in steps of 2, not 1.

• 不包括结尾值。这与切片类似。
• start 是可选的 ， 默认值是 0
• step 是可选的，默认值是 1
• 当需要的值时候 range()才计算值，实际上，它不存储大范围的数。

### enumerate() 函数

enumerate 函数为迭代添加一个额外的计数值。

names = ['Elwood', 'Jake', 'Curtis']
for i, name in enumerate(names):
# Loops with i = 0, name = 'Elwood'
# i = 1, name = 'Jake'
# i = 2, name = 'Curtis'


with open(filename) as f:
for lineno, line in enumerate(f, start=1):
...


enumerate可以看成以下语句的简写：

i = 0
for x in s:
statements
i += 1


### For 与元组

points = [
(1, 4),(10, 40),(23, 14),(5, 6),(7, 8)
]
for x, y in points:
# Loops with x = 1, y = 4
#            x = 10, y = 40
#            x = 23, y = 14
#            ...


### zip() 函数

zip 函数采用多个序列，并且生成将它们组合在一起的迭代器。

columns = ['name', 'shares', 'price']
values = ['GOOG', 100, 490.1 ]
pairs = zip(columns, values)
# ('name','GOOG'), ('shares',100), ('price',490.1)


for column, value in pairs:
...


zip 函数的常见用法是创建用于构造字典的键值对。

d = dict(zip(columns, values))


## 练习

### 练习 2.13：计数

>>> for n in range(10):            # Count 0 ... 9
print(n, end=' ')

0 1 2 3 4 5 6 7 8 9
>>> for n in range(10,0,-1):       # Count 10 ... 1
print(n, end=' ')

10 9 8 7 6 5 4 3 2 1
>>> for n in range(0,10,2):        # Count 0, 2, ... 8
print(n, end=' ')

0 2 4 6 8
>>>


### 练习 2.14：更多序列操作

>>> data = [4, 9, 1, 25, 16, 100, 49]
>>> min(data)
1
>>> max(data)
100
>>> sum(data)
204
>>>


>>> for x in data:
print(x)

4
9
...
>>> for n, x in enumerate(data):
print(n, x)

0 4
1 9
2 1
...
>>>


>>> for n in range(len(data)):
print(data[n])

4
9
1
...
>>>


### 练习 2.15：enumerate() 函数使用示例

>>> cost = portfolio_cost('Data/missing.csv')
Row 4: Couldn't convert: ['MSFT', '', '51.23']
Row 7: Couldn't convert: ['IBM', '', '70.44']
>>>


...
for rowno, row in enumerate(rows, start=1):
try:
...
except ValueError:


### 练习 2.16：使用 zip() 函数

Data/portfolio.csv 文件中，第一行包含列标题。在之前所有代码中，我们把它丢弃了。

>>> f = open('Data/portfolio.csv')
['name', 'shares', 'price']
>>>


>>> row = next(rows)
>>> row
['AA', '100', '32.20']
[ ('name', 'AA'), ('shares', '100'), ('price', '32.20') ]
>>>


>>> record = dict(zip(headers, row))
>>> record
{'price': '32.20', 'name': 'AA', 'shares': '100'}
>>>


# pcost.py

def portfolio_cost(filename):
...
for rowno, row in enumerate(rows, start=1):
try:
nshares = int(record['shares'])
price = float(record['price'])
total_cost += nshares * price
# This catches errors in int() and float() conversions above
except ValueError:
...


name,date,time,shares,price
"AA","6/11/2007","9:50am",100,32.20
"IBM","5/13/2007","4:20pm",50,91.10
"CAT","9/23/2006","1:30pm",150,83.44
"MSFT","5/17/2007","10:30am",200,51.23
"GE","2/1/2006","10:45am",95,40.37
"MSFT","10/31/2006","12:05pm",50,65.10
"IBM","7/9/2006","3:15pm",100,70.44

>>> portfolio_cost('Data/portfoliodate.csv')
44671.15
>>>


### 练习 2.17：翻转字典

>>> prices = {
'GOOG' : 490.1,
'AA' : 23.45,
'IBM' : 91.1,
'MSFT' : 34.23
}
>>>


>>> prices.items()
dict_items([('GOOG', 490.1), ('AA', 23.45), ('IBM', 91.1), ('MSFT', 34.23)])
>>>


>>> pricelist = list(zip(prices.values(),prices.keys()))
>>> pricelist
[(490.1, 'GOOG'), (23.45, 'AA'), (91.1, 'IBM'), (34.23, 'MSFT')]
>>>


>>> min(pricelist)
(23.45, 'AA')
>>> max(pricelist)
(490.1, 'GOOG')
>>> sorted(pricelist)
[(23.45, 'AA'), (34.23, 'MSFT'), (91.1, 'IBM'), (490.1, 'GOOG')]
>>>


zip() 函数经常应用于需要从不同的地方把数据进行配对。例如，为了使用已命名的值构建字典，将列名和列值进行配对。

>>> a = [1, 2, 3, 4]
>>> b = ['w', 'x', 'y', 'z']
>>> c = [0.2, 0.4, 0.6, 0.8]
>>> list(zip(a, b, c))
[(1, 'w', 0.2), (2, 'x', 0.4), (3, 'y', 0.6), (4, 'z', 0.8))]
>>>


>>> a = [1, 2, 3, 4, 5, 6]
>>> b = ['x', 'y', 'z']
>>> list(zip(a,b))
[(1, 'x'), (2, 'y'), (3, 'z')]
>>>

posted @ 2021-02-23 22:45  codists  阅读(189)  评论(0编辑  收藏  举报