切片
我们之前学习,取某一个集合里面的元素是根据键值一一存取,但是如果一个集合里面有n个元素,而我们需要在这个里面取1000个元素,还继续一一取的话,可能会有点复杂,在python里面为我们提供了切片(Slice)操作符,能简化我们的操作。
(1)先看之前的处理方法:
L=['a','s','d','f']
print(L[0],L[1],L[2])

因为我们集合中的元素比较少,可能存取还是比较容易的,如果多一点,可能就麻烦了。
(2)简化后的处理方法:
L=['a','s','d','f']
print(L[0:3])

个人感觉,方法还是很好的,因为通过输出的结果可以看出来,第二个直接是集合。
解析:L[0:3]这个意思就是说,从索引0开始,取3个元素。我们通常第一个索引都是0,这个时候我们还可以省略0,写成这样L[:3]
(3)从后面取也类似:
L=['a','s','d','f']
print(L[-2:])

切片还是非常好用的。
先创建一个1~100的数列:
L=list(range(1,101))
然后取前10个数:
print(L[:10])

后10个:
print(L[-10:])

取前11~12个数:
print(L[10:20])

前10个数字,取隔两个一取:
print(L[:10:2])

隔五个:
print(L[:10:5])

如果什么都不写,都可以复制一个list:
print(L[:])

我们之前操作的都是list集合,tuple也是可以,只是操作之后,结果仍然是tuple;
迭代
在java里面我们一般是for进行遍历的,这种遍历我们称为迭代;
在python中我们是通过for...in来完成的,一般情况下,我们的遍历都是通过下标完成的,而python中,只要是迭代对象,都是可以的;
d={'a':1,'b':2,'c':3}
for key in d:
print(key)

在默认情况下,一般都是迭代key,如果想要迭代value,可以用for value in d.values(),如果需要同时迭代key和value。可以用for k, v in d.items()。
字符串也是可以迭代的:
for chr in 'ABC':
print(chr)

如果你想迭代一个对象,但是你不知道他是不是可以迭代,这个也是可以判断的:
from collections import Iterable
print(isinstance(123,Iterable))//整数不可以迭代

from collections import Iterable
print(isinstance('acf',Iterable))//字符串可以迭代

循环一个类似java一样的下标循环,python
有一个内置函数enumerate可以把一个list变成索引-元素对,这样就可以在for循环中同时迭代索引和元素本身:
l=['B','D','A']
for i,value in enumerate(l):
print(i,value)

在上面的代码中我们使用了两个变量,这个是很常见的,比如:
l=[(1,1),(2,2),(3,3)]
for x,y in l:
print(x,y)

根据情况需要,适当使用吧。
练习:请使用迭代查找一个list中最小和最大值,并返回一个tuple:
L=[7,2,3,5,6]
def findMinAndMax(L):
if L != []:
max = L[0]
min = L[0]
for l in L:
if max < l:
max = l
if min > l:
min = l
return (min, max)
else:
return (None, None)
print(findMinAndMax(L))

列表生成式
如果我们需要创建一个1~100的数组,如果一个一个去输入的话,工作量还是很大的,这时候我们就可以使用range这个函数来创建:
print(list(range(1,101)))

可以在控制台看到,他是从1开始100结束的一个集合。
如果是要生成[1x1, 2x2, 3x3, ..., 10x10]这个的呢?
L=[]
for x in range(1,11):
L.append(x*x)
print(L)

其实还是有更加简单的:
L=[x * x for x in range(1,11)]
print(L)

两层循环:
L=[m+n for m in 'ABC' for n in 'ERF']
print(L)

for循环同时使用两个或者多个变量,比如dict和item()可以同时迭代key和value:
d={'a':1,'s':2,'d':1,'aa':5}
for x,y in d.items():
print(x,"=",y)

列表生成式也可以使用两个变量来生成list:
d={'a':1,'s':2,'d':1,'aa':5}
print([x+'='+y for x,y in d.items()])


我们看到的报错通过翻译应该就名报了,他这里只能是string到string,可是上面的代码明显不是。
d={'a':'A','s':'S','d':'D','aa':'AA'}
print([x+'='+y for x,y in d.items()])

通过修改后,我们可以发现成了;
最后就是把一个list中的所有字符串改成小写:
L = ['Hello', 'World', 'IBM', 'Apple']
print([s.lower() for s in L])

练习:
请修改列表生成式,通过添加if语句保证列表生成式能正确地执行:
预期结果是L2 = ['hello', 'world', 'apple']
L1 = ['Hello', 'World', 18, 'Apple', None]
L2=[s.lower() for s in L1 if isinstance(s,str)]
print(L2)

生成器
生成器就是比如这个有一个1~10000000000的一个集合,元素的顺序是不固定的,但是我现在就是要取其中为56的元素,可是我并不知道他在哪里,这时候就可以使用生成器了;
生成器其实就是一个generator,我们一般创建的集合是List,但是生成器并不是,其实只需要把【】改成()就好了,打印我们所需要为56的元素用的是next()函数获取generator的下一个返回值,如果只是一路next()的话,可能他是从第一个开始打印,直到找到56,可能你才会停止,这样比较麻烦,这里还有一个函数就是yield,我感觉他有点像java里面的case,如果他是56,直接打印,不是,就直接中断,不需要打印。
def fib(max):
n, a, b = 0, 0, 1
while n < max:
yield b
a, b = b, a + b
n = n + 1
return 'done'

迭代器
我们已经知道,可以直接作用于for循环的数据类型有以下几种:
一类是集合数据类型,如list、tuple、dict、set、str等;
一类是generator,包括生成器和带yield的generator function。
这些可以直接作用于for循环的对象统称为可迭代对象:Iterable。
可以使用isinstance()判断一个对象是否是Iterable对象:
from collections import Iterable
print(isinstance([],Iterable))

from collections import Iterable
print(isinstance(100,Iterable))

而生成器不但可以作用于for循环,还可以被next()函数不断调用并返回下一个值,直到最后抛出StopIteration错误表示无法继续返回下一个值了。
可以被next()函数调用并不断返回下一个值的对象称为迭代器:Iterator。
可以使用isinstance()判断一个对象是否是Iterator对象:
>>> from collections import Iterator
>>> isinstance((x for x in range(10)), Iterator)
True
>>> isinstance([], Iterator)
False
>>> isinstance({}, Iterator)
False
>>> isinstance('abc', Iterator)
False
生成器都是Iterator对象,但list、dict、str虽然是Iterable,却不是Iterator。
把list、dict、str等Iterable变成Iterator可以使用iter()函数:
>>> isinstance(iter([]), Iterator)
True
>>> isinstance(iter('abc'), Iterator)
True
小结
凡是可作用于for循环的对象都是Iterable类型;
凡是可作用于next()函数的对象都是Iterator类型,它们表示一个惰性计算的序列;
集合数据类型如list、dict、str等是Iterable但不是Iterator,不过可以通过iter()函数获得一个Iterator对象。
Python的for循环本质上就是通过不断调用next()函数实现的,例如:
for x in [1, 2, 3, 4, 5]:
pass
实际上完全等价于:
# 首先获得Iterator对象:
it = iter([1, 2, 3, 4, 5])
# 循环:
while True:
try:
# 获得下一个值:
x = next(it)
except StopIteration:
# 遇到StopIteration就退出循环
break
网友评论