Merging Or Reversing N-grams To A Single String
How do I merge the bigrams below to a single string? _bigrams=['the school', 'school boy', 'boy is', 'is reading'] _split=(' '.join(_bigrams)).split() _newstr=[] _filter=[_newstr.a
Solution 1:
# Multi-for generator expression allows us to create a flat iterable of words
all_words = (word for bigram in _bigrams for word in bigram.split())
defno_runs_of_words(words):
"""Takes an iterable of words and returns one with any runs condensed."""
prev_word = Nonefor word in words:
if word != prev_word:
yield word
prev_word = word
final_string = ' '.join(no_runs_of_words(all_words))
This takes advantage of generators to lazily evaluate and not keep the entire set of words in memory at the same time until generating the one final string.
Solution 2:
If you really wanted a oneliner, something like this could work:
' '.join(val.split()[0] for val in (_bigrams)) + ' ' + _bigrams[-1].split()[-1]
Solution 3:
Would this do it? It does simply take the first word up to the last entry
_bigrams=['the school', 'school boy', 'boy is', 'is reading']
clause = [a.split()[0] if a != _bigrams[-1] else a for a in _bigrams]
print' '.join(clause)
Output
the school boy is reading
However, concerning performance probably Amber's solution is a good option
Post a Comment for "Merging Or Reversing N-grams To A Single String"