迭代协议:
类实现方法__next__,则可以使用for、while来实现迭代
使用iter或者next实现手工迭代:
L = [1,2,3] I = iter(L) print(I.next())
列表推导式:
L = [x + y for x in ‘abc‘ for y in ‘lmn‘] print(L) [‘al‘, ‘am‘, ‘an‘, ‘bl‘, ‘bm‘, ‘bn‘, ‘cl‘, ‘cm‘, ‘cn‘]
2024-01-04 14:31:22
迭代协议:
类实现方法__next__,则可以使用for、while来实现迭代
使用iter或者next实现手工迭代:
L = [1,2,3] I = iter(L) print(I.next())
列表推导式:
L = [x + y for x in ‘abc‘ for y in ‘lmn‘] print(L) [‘al‘, ‘am‘, ‘an‘, ‘bl‘, ‘bm‘, ‘bn‘, ‘cl‘, ‘cm‘, ‘cn‘]