欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

IteratorandGenerator

# Iterator  一個對象,代表了遺傳數(shù)據(jù)流,使用__next__()方法或內(nèi)置函數(shù)next()
# 返回連續(xù)的對象,沒有數(shù)據(jù)返回時,拋出StopIteration異常

# iterable  一個對象,能每次返回數(shù)據(jù)組中的一個成員  for 循環(huán)中每次返回一個值 或
# 內(nèi)置函數(shù)iter()傳入?yún)?shù)返回iterator對象

# generator  使用了yield或者生成器表達式,申城iterator對象  用一種方便的
# 方法實現(xiàn)了iterator,在for循環(huán)取數(shù)據(jù)或使用next()取數(shù)據(jù)
# Iterator和Generator的關(guān)系
# 針對函數(shù)的列表進行優(yōu)化
# 針對讀取大文件進行優(yōu)化
def gen_num():
    yield 8
    yield 99
    yield 999
g = gen_num()
g
<generator object gen_num at 0x0000014F3F39EC50>
next(g) # 第一次運行
---------------------------------------------------------------------------

StopIteration                             Traceback (most recent call last)

<ipython-input-12-787e36cb69a2> in <module>()
----> 1 next(g) # 第一次運行

StopIteration: 
def gen_num2():
    for i in range(8):
        yield  i  # yield有類似返回值return的效果
for g in gen_num2():
    print(g)  # 每次取一個值的時候,才生成這個值,節(jié)省內(nèi)存
0
1
2
3
4
5
6
7
my_list = [1,2,3]
next(my_list)  # 用 next方法驗證是否為迭代器
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-17-4a97765cd86f> in <module>()
----> 1 next(my_list)

TypeError: 'list' object is not an iterator
next(iter(my_list))
1
next(iter(my_list))
1
it = iter(my_list)
next(it)
1
next(it)
2
from collections import Iterator, Iterable
isinstance(my_list, Iterator)
False
isinstance(iter(my_list), Iterator)
True
isinstance((), Iterator)
False
isinstance((), Iterable)  # 元組不是迭代器,但是可迭代
True
isinstance(range(8), Iterable)
True
isinstance(range(8), Iterator)
False

針對函數(shù)的列表進行優(yōu)化

# Python 3:Fibonacci series up to n 
def fib(n):
    a, b = 0, 1
    while a < n:
        print(a, end='')
        a, b = b, a+b
        print()

fib(9)
0
1
1
2
3
5
8
def fil_list(n):
    a, b = 0, 1
    result = []
    while a < n:
        result.append(a) 
        a, b = b, a+b
    return result
fil_list(55)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
fil_list(1000)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
def fib_gen(n):
    a, b = 0, 1
    while a< n:
        yield a
        a, b = b, a+b

fib_gen(80)
<generator object fib_gen at 0x0000014F3EA593B8>
for i in fib_gen(80):
    print(i)
0
1
1
2
3
5
8
13
21
34
55
[i for i in fib_gen(66)]  # 列表推導式
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

針對讀取大文件進行優(yōu)化

import os
base_dir = r'D:\全棧\全棧資料\第三階段 python進階\文件與日志-演示代碼\02-auto\data'
log_path = os.path.join(base_dir, 'access.log')
log_file = open(log_path)
log_data = log_file.readlines()
log_file.close()
len(log_data)
864
def read_log(log_path):
    with open(log_path) as f:
        print('Iterator:', isinstance(f, Iterator))  # open方法已經(jīng)為我們生成一個迭代器
        for line in f:
            print(line)
            break

read_log(log_path)
Iterator: True
220.181.7.76 - - [20/May/2010:07:26:23 +0100] "GET / HTTP/1.1" 200 29460 "-" "Baiduspider+(+http://www.baidu.com/search/spider.htm)"

列表推導式

[i for i in fib_gen(6)]
[0, 1, 1, 2, 3, 5]
m = [i for i in fib_gen(6)]
for i in m:
    print(i)
0
1
1
2
3
5

生成器表達式

(i for i in fib_gen(5))
<generator object <genexpr> at 0x0000014F3EAC9C50>
n = (i for i in fib_gen(9))
for i in n:
    print(i)
0
1
1
2
3
5
8

名稱欄目:IteratorandGenerator
鏈接URL:http://aaarwkj.com/article34/jpdcse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、微信公眾號、域名注冊、網(wǎng)站設(shè)計公司、App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)
中文字幕av一区二区人妻| 国产老熟女高潮一区二区| 精品欧美一区二区在线| 日韩一二三四区精品电影免费播放| 97成品视频在线播放| 国一区二区三区四区av| 久久国产精品亚洲熟女66r| 天天操天天夜夜操夜夜| 蜜臀在线观看免费视频| 日本女优久久精品观看| 操你啦夜夜操狠狠躁天天爽| 久久久精品国产亚洲av日韩| 欧美在线免费黄片视频| 亚洲免费av一区二区| 成人嚼牙特别黑黄怎么办| 成人免费视频观看国产| 国产精品成人av在线| 欧美精品一区二区网站| 高清欧美大片免费观看| 日韩成年人高清精品不卡一区二区| 91大片在线观看视频| 亚洲av成人一区二区三区| 欧美日韩中文字幕精品| 亚洲成人精品久久久 | 日韩一区二区三区免费播放| 少妇肥臀一区二区三区| 国产精品国产三级国产专播精品 | 欧美成人精品在线观看| 国产丝袜美腿一二三区| 日本伦理三级在线观看| 亚洲一区二区精品天堂| 亚洲精品一级二级三级| 溪乱毛片一区二区三区| 日本韩国精品视频在线| 97资源视频在线播放| 久久精品国产精品亚洲片| 黄色av手机在线观看| 偷窥偷拍视频一区二区| 亚洲黄色录像一区二区人妻黑人| 青青草视频在线好好热| 91麻豆精品国产自产|