0
List and append question
If There are three sentences ,how can I get the every single words of three sentences to be elements in one list ? Ex) txt file is .. Learning python is hard Summer is too hot Be careful not to be exhusted Output i want to see is.. ['learning','python','is','hard','Summer'... ...'exhusted'] I wrote as follow.. file=open(fname) #three sentences in this file ans=list() for line in file: sline=line.split() ans.append(sline) print ans The result is.. 3 elements having words in each sentence.. Help me out..I spent around 2hours to solve this but I almost give up ..
2 Answers
+ 1
Solution is as mentioned below:
file=open(fname) #three sentences in this file
ans=list()
for line in file:
sline=line.split()
for word in sline:
ans.append(word)
print ans
You should add another for loop to parse each word in the split line and then append it the list.
0
Awe. Thank you very much for your help!!