zoukankan      html  css  js  c++  java
  • Day7:Filesprocess,Iterator and Generator

    # r r+ w w+ a(add in the eend b+
    """

    # f = open('陈粒','r',encoding='utf-8')
    data = f.read()
    print(f.readable())
    print(f.writable())
    print(f.readline())
    # just read one line each time
    print(f.readlines())
    # read all the lines and put in a list
    print(data)
    f = open('陈粒1','r+',encoding='utf-8')
    f.write('I have seen the desert raining
    ')
    data = f.read()
    print(data)
    f.close()

    """

    with open('chenli','r',encoding='utf-8') as sdr_s,with open('chenli1','w',encoding='utf-8') as dtr_s:
    data = sdr_s.read()
    dtr_s.write(data)
    f = open('dtr_s','r',encoding='utf-8')
    data = f.read()
    print(data)

    #iterator and generator:satisfy the principle(__iter__(),means have the use of __next__()

    # 1:
    l = [1,2,3,4] iter_1 = l.__iter__() print(iter_1.__next__()) print(iter_1.__next__()) print(iter_1.__next__()) print(next(iter_1)) # next():the inner function of python;the same as iter_1.__next__() # 2: for i in l: #iter_1 = l.__iter__() print(iter_1.__next__()) print(i) # 3: print(l[2]) # 4:index index = 0 while index < len(l): print(l[index]) index += 1 # as for dict(return the key values) and files can also use the 'for' sentence

    # ***************generator is iterator :
    # 1:generator function(replace the return to yield and yield can retain the function's state)

    def test():
    print('start born:')
    print('start born:')
    print('start born:')
    yield 'me'
    print('born son')
    yield 'son'
    print('born grandson')
    yield 'grandson'
    g = test()
    print(g)
    print(g.__next__())
    print(g.__next__())
    print(g.__next__())

    # 2: ternary expression:

    # name = 'alex'
    name = 'zxver'
    res = 'SB' if name == 'alex' else 'beauty'
    print(res)
    l = ['egg%s' %i for i in range (10)]
    l1 = ['egg%s' %i for i in range (10) if i < 5]
    print(l)
    print(l1) 
    # use tuple
    l2 = ('egg%s' %i for i in range (10))
    print(l2)
    print(l2.__next__())
    print(l2.__next__())
    print(l2.__next__())
    print(l2.__next__())
    print(next(l2))

    # generator can save the memory: use tuple
    print(sum(i for i in range(10000000)))

    def produce_baozi():
    for i in range(100):
    # return i
    yield 'sale a baozi%s' %i
    
    pro_g = produce_baozi()
    baozi1 = pro_g.__next__()
    print(baozi1)
    baozi2 = pro_g.__next__()
    print(baozi2)
    baozi3 = pro_g.__next__()
    print(baozi3)
    # yield can return mutiple numbers and can use the return value directly
    def census():
    with open('pop.py') as f:
    for line in f:
    p = eval(line)
    yield p['population']
    c = census()
    #print(c.__next__())
    #print(c.__next__())
    #print(c.__next__())
    #print(c.__next__())
    all_pop = sum(i for i in c)
    print(all_pop)
    ###################calculate the rate:(no any return value cause the generator can just overall on time)
    for q in c:
    print(q/all_pop)

    # send(self,value),can send the value to the last yield

    def sen():
    print('start')
    # yield 1
    # firt = yield 1
    firt = yield
    print('the next',firt)
    yield 2
    s = sen()
    print(s.__next__())
    # print(s.__next__())
    # print(s.send(None))
    print(s.send('value'))

    # eat baozi:two separate section

    import time
    def consumer(name):
    print('I %s start to eat baozi' %name)
    while True:
    baozi = yield
    time.sleep(1)
    print('%s is eat %s' %(name,baozi))
    def producer():
    c1 = consumer('zxver')
    c2 = consumer('alex')
    c1.__next__()
    c2.__next__()
    # c1.send('baozi')
    for i in range(10):
    time.sleep(1)
    c1.send('baozi %s' %i)
    c2.send('baozi %s' %i)
    producer()
  • 相关阅读:
    LeetCode 40. 组合总和 II(Combination Sum II)
    LeetCode 129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)
    LeetCode 60. 第k个排列(Permutation Sequence)
    LeetCode 47. 全排列 II(Permutations II)
    LeetCode 46. 全排列(Permutations)
    LeetCode 93. 复原IP地址(Restore IP Addresses)
    LeetCode 98. 验证二叉搜索树(Validate Binary Search Tree)
    LeetCode 59. 螺旋矩阵 II(Spiral Matrix II)
    一重指针和二重指针
    指针的意义
  • 原文地址:https://www.cnblogs.com/zxver/p/12124397.html
Copyright © 2011-2022 走看看