I want to convert my string to a list and concatenate items only if the item is a text.
For example:
string = "123 text 4 text text text 800 text"
l = []
for i in string.split():
if i[0].isalpha:
l.append(i)
What I want
l = ["123", "text", "4", "text text text", "800", "text"]
What I have
l = ["123", "text", "4", "text", "text", "text", "800", "text"]
I want to concat all text items between numbers into one list item
import itertools as it
string = "123 text 4 text text text 800 text"
l = [" ".join(group)
for _, group in it.groupby(
string.split(), key=lambda s: s[0].isalpha())]
Result:
>>> l
['123', 'text', '4', 'text text text', '800', 'text']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.