Justine Lombardi
Justine Lombardi

Reputation: 1

Python create a sorted list from file using append remove duplicate words

I am currently working in a python course and I am lost on this after 6 hrs +. Assignment directs student to create a program where the user enters a file name and python opens the file and builds a sorted word list with out duplicates. Directions are very clear that For loop and append must be used. " 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."

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

Upvotes: 0

Views: 159

Answers (2)

Anand S Kumar
Anand S Kumar

Reputation: 91009

Not sure what the following loop is supposed to do -

for words in fh:
    if words in 1st:continue
        elif 1st.append

The above does not do anything because you have already exhausted the file fh before control reaches this part.

You should put an inner loop inside - for line in fh: - that goes over the words in words list one by one and appends to lst if its not already there.

Also, you should do lst.append(word)

Also, i do not think your if..elif block is valid syntax either.

You should be doing something like xample -

for line in fh:
    line = line.strip()
    words = line.split()
    for word in words:
        if word not in lst:
            lst.append(word)

Upvotes: 0

Matt
Matt

Reputation: 3852

It would be easier to just use the set() by itself, but this would be a good implementation per the assignment instructions. It's really fast compared to a list only version!

from collections import Set

def get_exclusive_list(fname):
    words = []
    with open(fname.txt, 'r') as file_d:
        data = file_d.read()
    [words.extend(li.split(' ')) for li in data.splitlines()]
    data = []
    already_seen = set()
    for thing in words:
        if thing not in already_seen:
            data.append(thing)
            already_seen.add(thing)
return data


# The better implementation
def get_exclusive_list_improved(fname):
    words = []
    with open(fname.txt, 'r') as file_d:
        data = file_d.read()
    [words.extend(li.split(' ')) for li in data.splitlines()]
    return list(set(words))

Upvotes: 0

Related Questions