将字符串拆分为迭代器

编程入门 行业动态 更新时间:2024-10-12 18:21:51
本文介绍了将字符串拆分为迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

python是否有内置(意味着在标准库中)对产生迭代器而不是列表的字符串进行拆分?我在考虑使用非常长的字符串而不需要消耗大部分字符串。

Does python have a build-in (meaning in the standard libraries) to do a split on strings that produces an iterator rather than a list? I have in mind working on very long strings and not needing to consume most of the string.

推荐答案

不直接拆分字符串,但 re 模块有 re.finditer() (以及任何已编译的正则表达式上相应的 finditer()方法) 。

Not directly splitting strings as such, but the re module has re.finditer() (and corresponding finditer() method on any compiled regular expression).

@Zero要求举个例子:

@Zero asked for an example:

>>> import re >>> s = "The quick brown\nfox" >>> for m in re.finditer('\S+', s): ... print(m.span(), m.group(0)) ... (0, 3) The (4, 9) quick (13, 18) brown (19, 22) fox

更多推荐

将字符串拆分为迭代器

本文发布于:2023-11-29 22:33:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647820.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   迭代

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!