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.
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)
Subreddit
Post Details
- Posted
- 2 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/learnprogra...