#手动迭代:iter和next
#python3.0提供了一个内置函数next,它会自动调用一个对象的next方法,给定一个
#可迭代对象x。调用next(x)等同于x,next()
l = [1, 2, 3]
print(iter(l) is l)
#print(l.next())
l = [1, 2, 3]
i = iter(l)
print(next(i))
print(i.next())
l = [1, 2, 3]
for x in l:
print(x**2, end=' ')
l = [1, 2, 3]
i = iter(l)
while True:
try:
j = next(i)
except StopIteration:
break
else:
print(j ** 2, end=' ')
d = {'a':1, 'b':2, 'c':3}
for k in d.keys():
print(k, d[k])
#python3中,字典自己有个迭代器
i = iter(d)
print(next(i))
print(next(i))
print(next(i))
for k in d:
print(k, d[k])
import os
p = os.popen('dir')
print(p.next())
#迭代协议也是需要将结果包装到一个list中才能看到值。可迭代对象一次返回一
a = range(5)
print(a)
i = iter(a)
print(next(i))
print(next(i))
print(list(range(5)))
l = []
for i in [1, 2, 3]:
l.append(i+1)
print(l)
print([i+1 for i in [1, 2, 3]])
with open('some.py', 'w') as f:
f.write('import os\n')
f.write('a = 1\n')
f.write('b = 2\n')
with open('some.py') as f:
print(f.read())
f = open('some.py')
line = f.readlines()
print(line)
print([l.rstrip() for l in line])
l = [line.rstrip() for line in open('some.py')]
print(l)
#字符串操作
print([line.upper() for line in open('some.py')])
lines = [line.rstrip() for line in open('some.py') if line[0] == 'a']
print(lines)
print([x+y for x in 'abc' for y in 'lmn'])
#列表解析,in成员测试,map内置函数,sorted和zip都使用了迭代协议
#当应用文件时,会自动扫描
upper = [line.upper() for line in open('some.py')]
print(upper)
print(list(map(str.upper, open('some.py'))))
print('a = 1\n' in open('some.py'))
print(sorted(open('some.py')))
print(list(zip(open('some.py'), open('some.py'))))
print(list(enumerate(open('some.py'))))
print(list(filter(bool, open('some.py'))))
import functools, operator
print(functools.reduce(operator.add, open('some.py')))
print(sum([1, 2, 3, 4, 5]))
print(any(['spam', '', 'mi']))
print(all(['spam', '', 'mi']))
print(max([1, 2, 3, 4, 5]))
print(min([1, 2, 3, 4, 5]))
print(max(open('some.py')))
print(min(open('some.py')))
print(list(open('some.py')))
print(tuple(open('some.py')))
print('&&'.join(open('some.py')))# (牛逼)
#import os
#&&a = 1
#&&b = 2
a, *b = open('some.py')
print(a)
print(b)
#import os
#
#['a = 1\n', 'b = 2\n']
print(set(open('some.py')))
print({line for line in open('some.py')})
print({ix: line for ix, line in enumerate(open('some.py'))})
print({line for line in open('some.py') if line[0] == 'a'})
print({ix: line for ix, line in enumerate(open('some.py')) if line[0] == 'a'})
def f(a, b, c):
print(a, b, c, sep = '&')
f(1, 2, 3)
f(*open('some.py'))
x , y = (1, 2), (3, 4)
print(list(zip(x, y)))
a, b = zip(*zip(x, y))
print(a)
print(b)
r = range(3)
i1 = iter(r)
print(next(i1))
print(next(i1))
i2 = iter(r)
print(next(i2))
print(next(i1))
z = zip((1, 2, 3), (10, 11, 12))
i1 = iter(z)
i2 = iter(z)
print(next(i1))
print(next(i1))
print(next(i2))
m = map(abs,(-1, 0, 1))
i1 = iter(m)
i2 = iter(m)
print(next(i1)) # 1
print(next(i1)) # 0
print(next(i1)) # 1
#print(next(i2)) # StopIteration
r = range(3)
r1, r2 = iter(r), iter(r)
print(next(r1)) # 0
print(next(r1)) # 1
print(next(r1)) # 2
print(next(r2)) # 0
d = {'a':1, 'b':2, 'c':3}
for k in sorted(d.keys()):
print(k, d[k], end=' ')
for k in sorted(d):
print(k, d[k], end = ' ')
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。