findall和finditer打印出来的结果不一样,为何?

JeanLove 2018-05-09 10:38:02

import re
print re.findall('car', 'Carry the bar to the car', re.I)
print re.finditer('car', 'Carry the bar to the car', re.I).next().groups()


我期待上面两个print语句打印出来的东西是一样的,但是实际上:

['Car', 'car']
()


后面一句什么也没有打印。我的理解错在哪里?
...全文
485 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
欢乐的小猪 2018-05-10
  • 打赏
  • 举报
回复
findall和finditer类似,只是这个返回生成器,所以效率更高些
import re
print(re.findall('car', 'Carry the bar to the car', re.I))
for m in re.finditer('car', 'Carry the bar to the car', re.I):
    print(m.group(0))

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧