python(反復器のスライス操作)

1037 ワード

f = open('CodingStyle.txt', 'r')

lines = f.readlines()  # readlines                  ,      ,         

# print(lines[100:300])

for line in f:
    print(line)  #          ,                 

f.seek(0)  #       

for line in f:
    print(line)  #       
# -------------------------------------------------------------------------------------------------

from itertools import islice

l = islice(f, 100, 300)  #     iterator:     100-300 
l2 = islice(f, 100, None)  #     iterator:     100      
l3 = islice(f, 500)  #     iterator:      500 list,      ,   start=0,end=500

for line in l:
    print(line)
# ---------------------------------------------------------------------------------------------

ln = range(20)

t = iter(ln)

for x in islice(t, 5, 10):
    print(x)          #   :5, 6, 7, 8, 9

print('-'*20)

for x in t:
    print(x)          #      t,       ,  :10, 11, 12, 13, 14, 15, 16, 17, 18, 19,

print(list(ln))  # ln