Confusion with split function in Python

Umer

I am trying to alphabetically sort the words from a file. However, the program sorts the lines, not the words, according to their first words. Here it is.

fname = raw_input("Enter file name: ")
fh = open(fname)
lst = list()
for line in fh:
    lst2 = line.strip()
    words = lst2.split()
    lst.append(words)
    lst.sort()
print lst

Here is my input file

But soft what light through yonder window breaks 
It is the east and Juliet is the sun 
Arise fair sun and kill the envious moon 
Who is already sick and pale with grief

And this is what I'm hoping to get

['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder'] 
Henry

The problem is that words is an array of your words from the split. When you append words to lst, you are making a list of arrays, and sorting it will only sort that list.

You want to do something like:

for x in words:
  lst.append(x)
lst.sort()

I believe

Edit: I have implemented your text file, this following code works for me:

inp=open('test.txt','r')
lst=list()
for line in inp:
   tokens=line.split('\n')[0].split() #This is to split away new line characters but shouldnt impact
   for x in tokens:
     lst.append(x)
lst.sort()
lst

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related