New filters on the Home Feed, take a look!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

1
Building a list of words
Post Body

Hi - relatively new to Python, and Im working on a program that will read a file and build a list of words. For each word on each line, it needs to check to see if the word is already in the list. If its not there, it will append it to the list. To finish it will sort the list in alphabetical order. Code is below - it currently just sorts alphabetically by line (meaning sort is based on lines and not individual words). I know its staring me right in the face, but can someone lead me to a correction? Thanks in advance

fname = input("Enter file name: ")

fh = open(fname,'r')

lst = list()

for line in fh:

line = line.rstrip()

words = line.split()

if words not in lst:

lst.append(words)

lst.sort()

if words in lst: continue

print(lst)

Author
Account Strength
60%
Account Age
2 years
Verified Email
Yes
Verified Flair
No
Total Karma
730
Link Karma
116
Comment Karma
614
Profile updated: 3 days ago
Posts updated: 1 year ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
2 years ago