2016-06-14 3 views
-2

Ich habe diese Liste teilen:Wie eine Liste von Text in eine Liste von Wörtern Python

["This is a set of words and it is not correct"] 

Ich möchte es so machen:

["This", "Is", "A"] ... 

Wie kann ich das tun

+0

Wollen Sie auf beliebigen Leerzeichen aufteilen oder nur ein einzelnes '“ „'? – erip

+0

Bitte googlen Sie dies: "Teilen Sie einen Satz in Worte" http://stackoverflow.com/questions/743806/split-string-into-a-list-in-python –

+2

Sie schreiben einige Python-Code. – jonrsharpe

Antwort

1

Sie können so tun.

a = "This is a set of words and it is not correct" 
[i.capitalize() for i in a.split()] 

wenn der Eingang ist list wie Sie in Ihrer Frage erwähnt.

a = ["This is a set of words and it is not correct"] 
[i.capitalize() for i in a[0].split()] 

Ausgabe

[ 'Das', 'I', 'A', 'Set', 'Von', 'Worten', 'Und', 'Es', " ist, 'Nicht', 'Correct']

0

Verwenden Sie die split() Methode

>>> foo = ["This is a set of words and it is not correct"] 
>>> foo[0].split() 
['This', 'is', 'a', 'set', 'of', 'words', 'and', 'it', 'is', 'not', 'correct'] 
3
"This is a set of words and it is not correct".title().split() 

Ausgabe:

['This', 'Is', 'A', 'Set', 'Of', 'Words', 'And', 'It', 'Is', 'Not', 'Correct'] 
+0

'str.title' ist ziemlich süß. – erip

Verwandte Themen