敢点我给你好看查看效果更好噢
def manual_iter():
with open('data_file/test1_3.txt') as f:
try:
while True:
line = next(f)
print(line,end=' ')
except StopIteration:
pass
manual_iter()
my name is flfl
love python
say hello
to the world
python nihao
yongyuan
yanthon
pythonnn
def manual_iter():
with open('data_file/test1_3.txt') as f:
while True:
line = next(f,None)
if line is None:
break
print(line,end=' ')
manual_iter()
my name is flfl
love python
say hello
to the world
python nihao
yongyuan
yanthon
pythonnn
items = [1,2,3]
it = iter(items)
next(it)
1
next(it)
2
next(it)
3
next(it)
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
in ()
----> 1 next(it)
StopIteration:
class Node:
def __init__(self,value):
self._value = value
self._children = []
def __repr__(self):
return 'Node({!r})'.format(self._value)
def add_child(self,node):
self._children.append(node)
def __iter__(self):
return iter(self._children)
root = Node(0)
child1 = Node(1)
child2 = Node(2)
root.add_child(child1)
root.add_child(child2)
for ch in root:
print(ch)
Node(1)
Node(2)
def frange(start,stop,increment):
x = start
while x < stop:
yield x
x += increment
for n in frange(0,4,0.5):
print(n,end=' , ')
0 , 0.5 , 1.0 , 1.5 , 2.0 , 2.5 , 3.0 , 3.5 ,
list(frange(0,1,0.125))
[0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875]
def countdown(n):
print("开始计数值:",n)
while n>0:
yield n
n -= 1
print('结束')
c = countdown(3)
c
next(c)
开始计数值: 3
3
next(c)
2
next(c)
1
next(c)
结束
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
in ()
----> 1 next(c)
StopIteration:
class Node:
def __init__(self,value):
self._value = value
self._children = []
def __repr__(self):
return 'Node({!r})'.format(self._value)
def add_child(self,node):
self._children.append(node)
def __iter__(self):
return iter(self._children)
def depth_first(self):
yield self
for c in self:
yield from c.depth_first()
root = Node(0)
child1 = Node(1)
child2 = Node(2)
root.add_child(child1)
root.add_child(child2)
child1.add_child(Node(3))
child1.add_child(Node(4))
child2.add_child(Node(5))
for ch in root.depth_first():
print(ch)
Node(0)
Node(1)
Node(3)
Node(4)
Node(2)
Node(5)
class Node2:
def __init__(self,value):
self._value = value
self._children = []
def __repr__(self):
return 'Node({!r})'.format(self._value)
def add_child(self,node):
self._children.append(node)
def __iter__(self):
return iter(self._children)
def depth_first(self):
return DepthFirstIterator(self)
class DepthFirstIterator(object):
def __init__():
self._node = start_node
self.children_iter = None
self._child_iter = None
def __iter__(self):
return self
def __next__():
if self._children_iter is None:
self._children_iter = iter(self._node)
return self._node
elif self._child_iter:
try:
nextchild = next(self._child_iter)
return nextchild
except StopIteration:
self._child_iter = None
return next(self)
else:
self._child_iter = next(self._children_iter).depth_first()
return next(self)
a = [1,2,3,4]
list(reversed(a))
[4, 3, 2, 1]
for x in reversed(a):
print(x,end=' ')
4 3 2 1
with open('data_file/test1_3.txt') as f:
for line in reversed(f):
print(line)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in ()
1 with open('data_file/test1_3.txt') as f:
----> 2 for line in reversed(f):
3 print(line)
TypeError: '_io.TextIOWrapper' object is not reversible
with open('data_file/test1_3.txt') as f:
for line in reversed(list(f)):
print(line,end=' ')
python nihao
to the world
say hello
love python
my name is flfl
class Countdown:
def __init__(self,start):
self.start = start
def __iter__(self):
n = self.start
while n > 0:
yield n
n -= 1
def __reversed__(self):
n = 1
while n < self.start:
yield n
n += 1
for rr in reversed(Countdown(5)):
print(rr,end=' ')
print()
for rr in Countdown(10):
print(rr,end=' ')
1 2 3 4
10 9 8 7 6 5 4 3 2 1
from collections import deque
class linehistory:
def __init__(self,lines, histlen=3):
self.lines = lines
self.history = deque(maxlen=histlen)
def __iter__(self):
for lineno,line in enumerate(self.lines,1):
self.history.append((lineno,line))
yield line
def clear(self):
self.history.clear()
with open ('data_file/test1_3.txt') as f:
lines = linehistory(f)
for line in lines:
if 'python' in line:
for lineno, hline in lines.history:
print('{}:{}'.format(lineno,hline),end=' ')
1:my name is flfl
2:love python
3:say hello
4: to the world
5: python nihao
with open('data_file/test1_3.txt') as f:
lines = linehistory(f)
next(lines)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in ()
1 with open('data_file/test1_3.txt') as f:
2 lines = linehistory(f)
----> 3 next(lines)
TypeError: 'linehistory' object is not an iterator
with open('data_file/test1_3.txt') as f:
lines = linehistory(f)
it = iter(lines)
print(next(it))
my name is flfl
def count(n):
i = 0
while iyield i
i += 1
c = count(10)
c[1:5]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in ()
6
7 c = count(10)
----> 8 c[1:5]
TypeError: 'generator' object is not subscriptable
import itertools
for x in itertools.islice(c,1,5):
print(x,end=' ')
1 2 3 4
c = list(count(10))
c[1:5]
[1, 2, 3, 4]