Reputation: 11
I am new in Python and I have the following problem to solve:
"Open the file sample.txt and read it line by line. For each line, split the line into a list of words using the split() method. The program should build a list of words. For each word on each line check to see if the word is already in the list and if not append it to the list. When the program completes, sort and print the resulting words in alphabetical order."
I have done the following code, with some good result, but I can't understand the reason my result appears to multiple list. I just need to have the words in one list.
thanks in advance!
fname = input("Enter file name: ")
fh = open(fname)
lst = list()
lst=fh.read().split()
final_list=list()
for line in lst:
if line in lst not in final_list:
final_list.append(line)
final_list.sort()
print(final_list)
Upvotes: 1
Views: 1725
Reputation: 9600
Your code has following problems as is:
if line in lst not in final_list
- Not sure what you are trying to do here. I think you expect this to go over all words in the line and check in the final_list
close()
methodYou need to read all the lines to a list and iterate over the list of lines and perform the splitting and adding elements to the list as:
fname = input("Enter file name: ")
fh = open(fname)
lst = list()
lst = fh.read().split()
final_list=list()
for word in lst:
if word not in final_list:
final_list.append(word)
final_list.sort()
print(final_list)
fh.close()
Upvotes: 0
Reputation: 312780
Your code is largely correct; the major problem is the conditional on your if
statement:
if line in lst not in final_list:
The expression line in lst
produces a boolean result, so this will end up looking something like:
if false not in final_list:
That will always evaluate to false
(because you're adding strings to your list, not boolean values). What you want is simply:
if line not in final_list:
Right now, you're sorting and printing your list inside the loop, but it would be better to do that once at the end, making your code look like this:
fname = input("Enter file name: ")
fh = open(fname)
lst = list()
lst=fh.read().split()
final_list=list()
for line in lst:
if line not in final_list:
final_list.append(line)
final_list.sort()
print(final_list)
I have a few additional comments on your code:
You don't need to explicitly initialize a variable (as in lst = list())
) if you're going to immediately assign something to it. You can just write:
fh = open(fname)
lst=fh.read().split()
On the other hand, you do need to initialize final_list
because
you're going to try to call the .append
method on it, although it
would be more common to write:
final_list = []
In practice, it would be more common to use a set to
collect the words, since a set
will de-duplicate things
automatically:
final_list = set()
for line in lst:
final_list.add(line)
print(sorted(final_list))
Lastly, if I were to write this code, it might look like this:
fname = input("Enter file name: ")
with open(fname) as fh:
lst = fh.read().split()
final_list = set(word.lower() for word in lst)
print(sorted(final_list))
Upvotes: 1