I have a list of tuples that has strings in it For instance:
[(\'this\', \'is\', \'a\', \'foo\', \'bar\', \'sentences\')
(\'is\', \'a\', \'foo\', \'bar\', \'
The list comprehension creates temporary strings. Just use ' '.join instead.
>>> words_list = [('this', 'is', 'a', 'foo', 'bar', 'sentences'),
... ('is', 'a', 'foo', 'bar', 'sentences', 'and'),
... ('a', 'foo', 'bar', 'sentences', 'and', 'i'),
... ('foo', 'bar', 'sentences', 'and', 'i', 'want'),
... ('bar', 'sentences', 'and', 'i', 'want', 'to'),
... ('sentences', 'and', 'i', 'want', 'to', 'ngramize'),
... ('and', 'i', 'want', 'to', 'ngramize', 'it')]
>>> new_list = []
>>> for words in words_list:
... new_list.append(' '.join(words)) # <---------------
...
>>> new_list
['this is a foo bar sentences',
'is a foo bar sentences and',
'a foo bar sentences and i',
'foo bar sentences and i want',
'bar sentences and i want to',
'sentences and i want to ngramize',
'and i want to ngramize it']
Above for loop can be expressed as following list comprehension:
new_list = [' '.join(words) for words in words_list]