# python中对象迭代与反迭代的技巧总结

import requests from collections import iterable, iterator # 气温迭代器 class weatheriterator(iterator): def __init__(self, cities): self.cities = cities self.index = 0 def getweather(self, city): r = requests.get(‘http://wthrcdn.etouch.cn/weather_mini?city=’ + city) data = r.json()[‘data’][‘forecast’][0] return ‘%s：%s , %s’ % (city, data[‘low’], data[‘high’]) def __next__(self): if self.index == len(self.cities): raise stopiteration city = self.cities[self.index] self.index += 1 return self.getweather(city) # 可迭代对象 class weatheriterable(iterable): def __init__(self, cities): self.cities = cities def __iter__(self): return weatheriterator(self.cities) for x in weatheriterable([‘北京’, ‘上海’, ‘广州’, ‘深圳’]): print(x)

c:\python\python35\python.exe e:/python-intensive-training/s2.py 北京：低温 21℃ , 高温 30℃ 上海：低温 23℃ , 高温 26℃ 广州：低温 26℃ , 高温 34℃ 深圳：低温 27℃ , 高温 33℃ process finished with exit code 0

python pn = primenumbers(1, 30) for k in pn: print(k) “ 输出结果text
2 3 5 7 11 13 17 19 23 29
“`

-将该类的__iter__方法实现生成器函数，每次yield返回一个素数

class primenumbers: def __init__(self, start, stop): self.start = start self.stop = stop def isprimenum(self, k): if k < 2: return false for i in range(2, k): if k % i == 0: return false return true def __iter__(self): for k in range(self.start, self.stop + 1): if self.isprimenum(k): yield k for x in primenumbers(1, 20): print(x)

c:\python\python35\python.exe e:/python-intensive-training/s3.py 2 3 5 7 11 13 17 19 process finished with exit code 0

class floatrange: def __init__(self, start, stop, step=0.1): self.start = start self.stop = stop self.step = step def __iter__(self): t = self.start while t = self.start: yield t t -= self.step print(“正相迭代—–“) for n in floatrange(1.0, 4.0, 0.5): print(n) print(“反迭代—–“) for x in reversed(floatrange(1.0, 4.0, 0.5)): print(x)

c:\python\python35\python.exe e:/python-intensive-training/s4.py 正相迭代—– 1.0 1.5 2.0 2.5 3.0 3.5 4.0 反迭代—– 4.0 3.5 3.0 2.5 2.0 1.5 1.0 process finished with exit code 0

from itertools import islice f = open(‘access.log’) # # 前500行 # islice(f, 500) # # 100行以后的 # islice(f, 100, none) for line in islice(f,100,300): print(line)

islice每次训话都会消耗之前的迭代对象

l = range(20) t = iter(l) for x in islice(t, 5, 10): print(x) print(‘第二次迭代’) for x in t: print(x)

c:\python\python35\python.exe e:/python-intensive-training/s5.py 5 6 7 8 9 第二次迭代 10 11 12 13 14 15 16 17 18 19 process finished with exit code 0

1、某班学生期末考试成绩，语文、数学、英语分别存储再3个列表中，同时迭代三个列表，计算每个学生的总分(并行)

2、某年纪有四个班，某次考试没班英语成绩分别存储在四个列表中，依次迭代每个列表，统计全学年成绩高于90分人数(串行)

from random import randint # 申城语文成绩，# 40人，分数再60-100之间 chinese = [randint(60, 100) for _ in range(40)] math = [randint(60, 100) for _ in range(40)] # 数学 english = [randint(60, 100) for _ in range(40)] # 英语 total = [] for c, m, e in zip(chinese, math, english): total.append(c + m + e) print(total)

c:\python\python35\python.exe e:/python-intensive-training/s6.py [232, 234, 259, 248, 241, 236, 245, 253, 275, 238, 240, 239, 283, 256, 232, 224, 201, 255, 206, 239, 254, 216, 287, 268, 235, 223, 289, 221, 266, 222, 231, 240, 226, 235, 255, 232, 235, 250, 241, 225] process finished with exit code 0

from random import randint from itertools import chain # 生成四个班的随机成绩 e1 = [randint(60, 100) for _ in range(40)] e2 = [randint(60, 100) for _ in range(42)] e3 = [randint(60, 100) for _ in range(45)] e4 = [randint(60, 100) for _ in range(50)] # 默认人数=1 count = 0 for s in chain(e1, e2, e3, e4): # 如果当前分数大于90，就让count+1 if s > 90: count += 1 print(count)

c:\python\python35\python.exe e:/python-intensive-training/s6.py 48 process finished with exit code 0

Posted in 未分类