Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion with split function in Python

Tags:

python

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'] 
like image 603
Umer Avatar asked Dec 15 '22 10:12

Umer


2 Answers

lst.append(words) append a list at the end of lst, it does not concatenates lst and words. You need to use lst.extend(words) or lst += words.

Also, you should not sort the list at each iteration but only at the end of your loop:

lst = []
for line in fh:
    lst2 = line.strip()
    words = lst2.split()
    lst.extend(words)
lst.sort()
print lst

If you don't want repeated word, use a set:

st = set()
for line in fh:
    lst2 = line.strip()
    words = lst2.split()
    st.update(words)
lst = list(st)
lst.sort()
print lst
like image 199
Holt Avatar answered Dec 27 '22 07:12

Holt


lst.append(words) is adding the list as a member to the outer list. For instance:

lst = []
lst.append(['another','list'])
lst ## [['another','list']]

So you're getting a nested list. Use .extend(...) instead:

fname = raw_input("Enter file name: ")
fh = open(fname)
lst = list()
for line in fh:
    lst2 = line.strip()
    words = lst2.split()
    lst.extend(words)
lst.sort()
print lst
like image 38
TayTay Avatar answered Dec 27 '22 08:12

TayTay