1

I have a text file with the following lyrics:

'First verse Im off till I'm off the island
i'm riding like autoban on autopilot
before I touch dirt I deal you all with kindness
my natual persona much worse'

I want to convert it to a list, then sort it alphabetically, but what I keep getting is: [[First', 'verse', 'Im', 'off', 'till, "I'm", 'off' , 'the', 'island'] [then it prints the third line], [then the second line], [then the fourth line]]

and then it puts it into a list within a list [[ text, text..., ]]

How do I print it so it prints in order in one list and is sorted in alphabetical order using python code? Here is what I have so far:

fname = raw_input("Enter file name: ")  #prompts user for .txt file
fhand = open(fname)     #reads file
NewList =  []    #creating new list to append to
for line in fhand:    #for the lines in the text file, read them
    line.rstrip()   # strip the white space
    words = line.split()   #split the txt file into string words

    for word in words:      #for every word in txt file
        if words not in NewList:  #if not in the NewList
                NewList.append(words) #then appended words to Newlist 

                NewList.sort()    #sort it alphabetically

print NewList   #then print the sorted list
5
  • 1
    You probably want to add a tag for language you are using (Python?) Commented Mar 27, 2015 at 15:54
  • 1
    CamelCase should not be used in variable names in Python. Commented Mar 27, 2015 at 15:59
  • do you want to get rid of the repeated words when putting them in the list such as I'm Commented Mar 27, 2015 at 16:08
  • I think you want NewList.extend(words) instead of NewList.append(words), if I understand you correctly. extend adds all the elements in the argument list to the existing list, where append adds the argument list itself to the existing list... Commented Mar 27, 2015 at 16:34
  • Yes, I also want to get rid of duplicate words.... Commented Mar 27, 2015 at 23:47

4 Answers 4

2

It's possible in a single line!

sorted(open('file.txt').readlines())

or, if you want it to be printed like a single file

print "".join(sorted(open('file.txt').readlines()))
Sign up to request clarification or add additional context in comments.

3 Comments

Where's the Enter file name: bit?
Even if a one-liner is cool, you should use the with statement to close the file properly.
Thank you so far for the responses. Well I want to keep it in a list bracket and sort it alphabetically so the output I desire is ['First', 'Im', 'I'm', 'island', 'off'. 'off', 'the', 'till', 'verse',....] and so on for the rest of the lines, all in one bracket--list.
0

This creates a set to remove duplicate words and then sort it to a list

fname = raw_input("Enter file name: ")
with open(fname ,"r") as f:
    l = sorted(set([word for line in f for word in line.split()]))

Comments

0

Solution in Python 3

fname = input("Enter file name: ")
fh = open(fname)
lst = list()
newlist=[]
for line in fh:
    lst+=line.split()
for item in lst:
    if item not in newlist:
        newlist.append(item)
newlist.sort()
print(newlist)

you're welcome

Comments

0
#For the second for loop, using continue will skip the already present word 
#in the list and go to the top of the loop for a new iteration.

fname = input("Enter file name: ")
handler = open(fname)
lst = list()
alst = list()

for line1 in handler :
    line1.rstrip()
    lst += line1.split()

for i in lst:
    if i not in alst:
        alst.append(i)
    else:
        continue
alst.sort()
print(alst)

1 Comment

@Tyler helpful ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.