x='hello' gxr=iter(x) gxr=x.__iter__()
print(next(gxr)) print(gxr.__next__())
travel () === __ iter__
next()===__next__
The feeling is to simplify command only. ,,, 10 lines of code, the simplified, with two command resolved, resulting in a new function
def l(): yield 1 yield 2 yield 3 g=l() print(g.__next__()) print(g.__next__())
This useful
name='alex' # name='linhaifeng' res='SB' if name == 'alex' else '帅哥' (res=('SB' (if name == 'alex') else '帅哥'))
print(res)
Analyzing the 10 lines of code, the simplified, two command solved by generating a new function (Method)
= B (I for I in Range (10)) # generator expressions (). Processing of large data, not the station memory A = [I for I in Range (10)] # listing [] Print (A) Print (B ) # Print (Next (A)) # Print (Next (A)) Print (Next (B)) Print (Next (B))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
<generator object <genexpr> at 0x0000019BC0709548>
0
1
Another, mainly to solve large quantities of data
print(sum(i for i in range(10) if i > 5 )) print(sum([i for i in range(10) if i > 5 ])) print([i for i in range(10) if i > 5 ]) print(i for i in range(10) if i > 5 )
打印
30 sum calculated
30 sum calculated
[6, 7, 8, 9] large data, total memory
<generator object <genexpr> at 0x000002013F3B9F48 > function supra, does not occupy memory
Today too sleepy ... sleep, get up early tomorrow to work