Reputation: 159
I have been using this stackoverflow for years, but have never posted questions personally. I hope my first question is not too naive.
I am trying to do some concatenation in a string. Let's say I have a string
string = "C S E majors in U S"
I want to make this string that looks something like
string = "CSE majors in US"
I thought about doing it by splitting the string into list, loop it, and check whether the element has a length equals to 1. If it has, check for next ones iteratively, and combine them at the end.
I was wondering whether there are better approaches to this. Since I am pretty new to python, it will be very nice to have some illustrations with codes.
Thanks in advance!
Upvotes: 0
Views: 47
Reputation: 798556
Nope.
>>> ' '.join((' ' if pred else '').join(seq) for pred, seq in itertools.groupby("C S E majors in U S".split(), lambda x: len(x) > 1))
'CSE majors in US'
Upvotes: 1