Python3 split () with generator

In Python3, many methods return iterator or generator objects (instead of lists or other heavy objects in python2).

However, I found that the split line still returns list instead of generator or iteator :

 ~$ python3 Python 3.2.2 (...) >>> type('abc d'.split()) <class 'list'> 

Is there a buildin to split the string using generator or iterator ?

(I know that we can split it into ourselves and write a good generator function. I'm curious if there is anything in the standard library or language for this)

+4
source share
1 answer

re.finditer opens from re module => Python Docs

In short:

"" Returns an iterator giving matching objects for all matching matches for the RE pattern in the string. The string is scanned from left to right, and matches are returned in the order found. Empty matches are included in the result if they do not relate to the start of another match. ""

I think he will do what you need. For instance:

 import re text = "This is some nice text" iter_matches = re.finditer(r'\w+', text) for match in iter_matches: print(match.group(0)) 
+3
source

Source: https://habr.com/ru/post/1414041/


All Articles