1

I have a list (list of list) something like this.

 [['oh', 'yea', 'makes', 'sense'], ['Estas', 'enfermedad', 'un', 'cargo', 'poltico', 'tu', 'como', 'pblico', 'jesuischarlieytal'], ['old', 'men', 'finally', 'date', 'sarcasmsun', 'mar', 'ist'], ['sarinas', 'chanted', 'peacefully', 'deny', 'hypocrisysat', 'mar', 'ist']]

I want to convert the lists into sentences

 [[oh yea makes sense], [Estas enfermedad un cargo poltico tu como pblico jesuischarlieytal'], [old men finally sarcasmsun mar ist]]

I tried the below code. However it is not working correctly

a = tokenized_sentences2[0]
print(a)

['oh', 'yea', 'makes', 'sense']


print ' '.join(word[0] for word in a)

Could someone please help me on this?

Sijo K
  • 103
  • 1
  • 2
  • 8
  • 2
    sentences = [ [' '.join(item)] for item in words] – Shiping Feb 25 '17 at 02:58
  • thank you for the response. This didnt work for me however. Notsure whether i am making any other mistake, I am getting answer as a list of list [['oh'], ['yea'], ['makes'], ['sense']] – Sijo K Feb 25 '17 at 03:02
  • 1
    Why are you asking for a list of all the sentences and then applying every solution you're given to only the first sentence? Use these solutions on `tokenized_sentences2`, not `a`. – TigerhawkT3 Feb 25 '17 at 03:04
  • @SijoK as others already gave answers, words should be a list of lists, not just a list, as you showed in your first example. – Shiping Feb 25 '17 at 03:16
  • Took 16 different Google searches but I finally found the duplicate! \o/ – TigerhawkT3 Feb 25 '17 at 03:20
  • @Shiping - Thanks for the response. As TigerhawkT3 mentioned i was applying wrongly. Extremely sorry for the inconvenience caused. Very new to python. – Sijo K Feb 25 '17 at 03:24
  • @TigerhawkT3 - I am extremely sorry. I am new to Stackoverflow as well. Henceforth i will not repeat this mistake. – Sijo K Feb 25 '17 at 03:26
  • @SijoK no problem. everybody went through the learning curve. that's why we come here, to help if we can, and ask questions ourselves. – Shiping Feb 25 '17 at 03:49
  • @Shiping - Thanks again. – Sijo K Feb 25 '17 at 05:32

2 Answers2

14

If you want to get list of list,you can try this:

c=[['oh', 'yea', 'makes', 'sense'], ['Estas', 'enfermedad', 'un', 'cargo', 'poltico', 'tu', 'como', 'pblico', 'jesuischarlieytal'], ['old', 'men', 'finally', 'date', 'sarcasmsun', 'mar', 'ist'], ['sarinas', 'chanted', 'peacefully', 'deny', 'hypocrisysat', 'mar', 'ist']]

print [[' '.join(i)] for i in c]

Output:

[['oh yea makes sense'], ['Estas enfermedad un cargo poltico tu como pblico jesuischarlieytal'], ['old men finally date sarcasmsun mar ist'], ['sarinas chanted peacefully deny hypocrisysat mar ist']]

If you want to get list of sentences,you can just use this:

print [' '.join(i) for i in c]

Output:

['oh yea makes sense', 'Estas enfermedad un cargo poltico tu como pblico jesuischarlieytal', 'old men finally date sarcasmsun mar ist', 'sarinas chanted peacefully deny hypocrisysat mar ist']

join takes an iterable(usually a list) as an argument.

Concatenate a list or tuple of words with intervening occurrences of sep. The default value for sep is a single space character. It is always true that string.join(string.split(s, sep), sep) equals s.

McGrady
  • 10,869
  • 13
  • 47
  • 69
  • Not sure wheteher i am wrong. It still gives me the same answer. print [''.join(i) for i in a] answer - ['oh', 'yea', 'makes', 'sense'] – Sijo K Feb 25 '17 at 02:59
  • @SijoK I updated my answer. – McGrady Feb 25 '17 at 03:02
  • Sorry the updated answer leads to list of list. Here is the code i used - print [[''.join(i)] for i in a] Answer is - [['oh'], ['yea'], ['makes'], ['sense']] – Sijo K Feb 25 '17 at 03:03
  • if you're using python3 make sure use `print()` – mtkilic Feb 25 '17 at 03:04
  • @MahmutKilic - yes you are right. I am using like this only. print ([[''.join(i)] for i in a]) Answer - [['oh'], ['yea'], ['makes'], ['sense']] – Sijo K Feb 25 '17 at 03:06
  • Your original version before the edit was correct. The OP doesn't know how to apply the solution they've been handed. – TigerhawkT3 Feb 25 '17 at 03:06
  • @TigerhawkT3 ..... Maybe I should write both of them. – McGrady Feb 25 '17 at 03:09
  • @TigerhawkT3 - My bad, I am sorry. You are right. I should have applied sentence2 directly. I am very new to python. Hence the confusion. Sorry about that. – Sijo K Feb 25 '17 at 03:22
  • @McGrady - This answer is correct. As TigerhawkT3 mentioned i was applying wrongly. Extremely sorry for the inconvenience caused to you. – Sijo K Feb 25 '17 at 03:23
1
>>> mylist = [['some', 'sentence', 'here'],
      ['Otra', 'oracion', 'aca', 'tambien'],
      ['Ich', 'bin', 'ein', 'Ingenieur'],]

>>> [[' '.join(i)] for i in mylist]

output:

[['some sentence here'],
 ['Otra oracion aca tambien'],
 ['Ich bin ein Ingenieur']]
Pablo Reyes
  • 3,073
  • 1
  • 20
  • 30