文件迭代
>>> for line in open('e:\code\log\log.txt'):
print(line.upper(),end='')
13 IS PRIME
11 IS PRIME
7 IS PRIME
5 IS PRIME
3 IS PRIME
2 IS PRIME
>>> f=open('e:\code\log\log.txt')
>>> f.readline()
'13 is prime\n'
>>> f.readline()
'11 is prime\n'
>>> f.__next__()
'7 is prime\n'
>>>
map迭代
>>> M=map(abs,(-1,-2,0,9))
>>> M
<map object at 0x000001E340D612C8>
>>> next(M)
1
>>> next(M)
2
>>> next(M)
0
>>> next(M)
9
>>> next(M)
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
next(M)
StopIteration
>>> M=map(abs,(-1,-2,0,9))
>>> for x in M:
print(x)
1
2
0
9
>>> list(map(abs,(-1,0,1)))
[1, 0, 1]
>>>
>>> d=dict(a=2,b=3,c=4)
>>> d
{'a': 2, 'b': 3, 'c': 4}
>>> k=d.key()
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
k=d.key()
AttributeError: 'dict' object has no attribute 'key'
>>> k=d.keys()
>>> k
dict_keys(['a', 'b', 'c'])
>>> i=iter(k)
>>> next(i)
'a'
>>> for i in d.keys():print(i,end='')
abc
>>>