python : itertools 中的 islice : 获取迭代器结果的切片,消耗迭代器

本文介绍了islice函数,它可创建迭代器,生成项的方式类似切片返回值,能跳过前start个项,在stop处停止,step指定步幅。与切片不同,其start、stop和step不用负值。还给出了创建文本文档及读取文档前三行的示例,同时提到islice会消耗迭代器。
部署运行你感兴趣的模型镜像

islice(iterable, [start, ] stop [, step]):
创建一个迭代器,生成项的方式类似于切片返回值: iterable[start : stop : step],将跳过前start个项,迭代在stop所指定的位置停止,step指定用于跳过项的步幅。与切片不同,负值不会用于任何start,stop和step,如果省略了start,迭代将从0开始,如果省略了step,步幅将采用1.https://blog.youkuaiyun.com/larykaiy/article/details/82934527
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

示例:

step1:创建文本文档

my_file.txt

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#  
#  Copyright 2019 wzg16 <wzg16@wzg16-linux>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.

step2:读取文档前三行

read1.py


with open("my_file.txt") as f:
	lines = f.readlines()
	print(lines[0:3])

"""
返回:
['#!/usr/bin/env python\n', '# -*- coding: utf-8 -*-\n', '#\n']
"""

read_islice.py

from itertools import islice

	
with open("my_files.txt") as f2:
	for i in islice(f2,0,3):
		print(i)
	result = islice(f2,0,3)
	print(result)
	print(list(result))

"""
输出:
#!/usr/bin/env python

# -*- coding: utf-8 -*-

#  

<itertools.islice object at 0x7f52a94dedb8>
['#  Copyright 2019 wzg16 <wzg16@wzg16-linux>\n', '#  \n', '#  This program is free software; you can redistribute it and/or modify\n']
"""

islice 会消耗迭代器:

示例:

from itertools import islice
	
l = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]
it = iter(l)

slice1 = islice(it,0,3)
slice2 = islice(it,0,3)
slice3 = islice(it,3,6)
slice4 = islice(it,0,3)

print("slice1 = ",list(slice1))
print("slice2 = ",list(slice2))
print("slice3 = ",list(slice3))
print("slice4 = ",list(slice4))

"""
输出:
slice1 =  [1, 2, 3]
slice2 =  [4, 5, 6]
slice3 =  [10, 11, 12]
slice4 =  [13, 14, 15]

"""

 

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值