python - split 方法如何工作?
有人知道 python 中的 split 函数是如何工作的吗?我的意思是,它是逐个字符读取字符串,然后评估代码还是有另一种工作方式?我已阅读该文档,但它没有提及。
编辑
对于那些像我一样好奇的人,只需检查 这里。正如 Chris 所说,它应该在第 147 行。
Anyone knows how does the split function work in python? I mean, does it read the string char by char and then evaluates the code or it has another way of working? I've read the doc but it doesn't mention.
EDIT
For those who are curious like me, just check here. It should be on the 147th line as Chris stated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你查看Python源代码(我使用的是2.7.1,但我怀疑3.x系列中的位置发生了变化),完整的实现可以在
$src_dir/Objects/stringlib/split.h< /代码>。该函数的名称是
stringlib_split
,在 2.7.1 中可以在第 147 行找到。If you check out the python source code (I used 2.7.1, but I doubt the location changed in the 3.x series), the full implementation can be found at
$src_dir/Objects/stringlib/split.h
. The function's name isstringlib_split
and in 2.7.1 can be found on line 147.从 3.2 开始,有多种
split()
实现。首先,不带参数的split()
有自己的实现,因为它的语义与其他拆分略有不同。当给出分割字符串时,有两种可能的实现:一种用于单个字符分隔符,另一种用于其他字符串。单字符实现只是扫描字符串并将块附加到列表中。对于较长的字符串,算法是相同的,但使用 Bloom 过滤器 执行搜索。Speaking as of 3.2, there are several
split()
implementations. First of all,split()
with no arguments has its own implementation, since it has slightly different semantics than other splits. When a split string is given, there are two possible implementations: one for a single character separator and one for other strings. The one character implementation simply scans through the string and appends chunks to a list. For longer strings, the algorithm is the same, but search performed with Bloom filters.