Iterate over slices of a string

def iter_slices(string, slice_length):
"""Iterate over slices of a string."""
pos = 0
if slice_length is None or slice_length <= 0:
slice_length = len(string)
while pos < len(string):
yield string[pos:pos + slice_length]
pos += slice_length

s = "thisisatest"
o = 2
p = iter_slices(s,o)
print p
print (p.next())
print (p.next())
print (p.next())
print (p.next())

原文地址:https://www.cnblogs.com/tnyleyon/p/9452844.html

时间: 2024-11-10 13:10:10

Iterate over slices of a string的相关文章

How to Iterate Map

常用iterate 方法 1 Map<Integer, String> m = new HashMap<Integer, String>(); 2 for(Map.Entry<Integer, String> entry : m.entrySet()){ 3 System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue()); 4 } 5 6

LeetCode 599. Minimum Index Sum of Two Lists (从两个lists里找到相同的并且位置总和最靠前的)

Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings. You need to help them find out their common interest with the least list index sum. If there is a choice tie betw

Span&lt;T&gt;

Introduction Span<T> is a new type we are adding to the platform to represent contiguous regions of arbitrary memory, with performance characteristics on par with T[]. Its APIs are similar to the array, but unlike arrays, it can point to either mana

Check iO:初学Python

The end of other For language training our Robots want to learn about suffixes. In this task, you are given a set of words in lower case. Check whether there is a pair of words, such that one word is the end of another (a suffix of another). For exam

【转】Struts1.x系列教程(7):Logic标签库

转载地址:http://www.blogjava.net/nokiaguy/archive/2009/01/archive/2009/01/archive/2009/01/archive/2009/01/archive/2009/01/archive/2009/01/archive/2009/01/19/251967.html Struts Logic标签库包含了如下三类标签:     1. 循环标签:iterate标签.用于枚举数组.集合类型对象中的元素. 2. 条件处理标签:用于是否相等.比

im4java开发向导

0.搜索ImageMagick下载安装 1.Setting up the Environment    引入im4java到classpath    设置图片处理引擎的command searchpath        三种方式:            设置系统环境变量 IM4JAVA_TOOLPATH            在程序中设置全局变量(将覆盖上一层) String myPath="C:\\Programs\\ImageMagick;C:\\Programs\\exiftool&quo

配置Hibernate二级缓存

首先找到配置EHCahe二级缓存需要添加的jar包 hibernate-release-4.1.9.Final→lib→optional→ehcache→下的ehcache-core-2.4.3.jar和hibernate-ehcache-4.1.9.Final.jar还有slf4j-api-1.6.1.jar! 然后再从hibernate-release-4.1.9.Final→project→etc→把ehcache.xml复制到src目录下.把缓存.xml没用的都删了,留下最终: <ehc

Levenshtein distance 编辑距离

编辑距离,又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数.许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符 实现方案: 1. 找出最长公共子串长度 参考代码: apache commons-lang public static int getLevenshteinDistance(CharSequence s, CharSequence t) { if (s == null || t == null) { throw new Il

C#操作Word (1)Word对象模型

Word对象模型  (.Net Perspective) 本文主要针对在Visual Studio中使用C# 开发关于Word的应用程序 来源:Understandingthe Word Object Model from a .NET Developer's Perspective 五大对象 Application           :代表Microsoft Word应用程序本身 Document            :代表一个Word文档 Selection              :