Jacob Gliddon WD
Jacob Gliddon WD

Reputation: 3

Can't get the lines to join up as one list, confused

Basically I need to join up 4 lines up text here's the brief(yes this is homework but I am so stuck): 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.

My code :

fname = input("Enter file name: ")
fh = open(fname)

lst = list()
for line in fh:
    lines = line.rstrip()
    for word in lines:
        word = lines.split()
        if lines in word: continue
    lst.append(word)
    lst.sort()
print(lst)

Expected Results:

['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 
'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 
'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 
'with', 'yonder']

Actual Results:

[['Arise', 'fair', 'sun', 'and', 'kill', 'the', 'envious', 'moon'], 
['But', 'soft', 'what', 'light', 'through', 'yonder', 'window', 
'breaks'], ['It', 'is', 'the', 'east', 'and', 'Juliet', 'is', 'the', 
'sun'], ['Who', 'is', 'already', 'sick', 'and', 'pale', 'with', 
'grief']]

Upvotes: 0

Views: 62

Answers (2)

tripleee
tripleee

Reputation: 189357

You are probably better off refactoring the script to avoid reusing the loop variable inside the loop, and also grouping things slightly differently for legibility and making the code idiomatic.

fname = input("Enter file name: ")

lst = list()

# Use a context manager (handles properly closing the file when done etc)
with open(fname) as fh:
    for line in fh:
        # Actually loop over split result
        for word in line.rstrip().split():
            # Compare lst, not word
            if word in lst: continue
            lst.append(word)
# Only sort when done
lst.sort()
print(lst)

For better usability, avoid having the script prompt interactively for a file name; instead, have it read the input file(s) as command-line argument{s) (hint: sys.argv[1:]).

Upvotes: 1

Primusa
Primusa

Reputation: 13498

You're getting a nested list since you're appending a list of words each time and not each individual word:

lst = []

for line in fh:
    for word in lines.split():
        if word not in lst: lst.append(word)

lst.sort()

Additionally you can also screen out the duplicates with a set()

s = set()
for line in fh:
    for word in lines.split():
        s.add(word)
lst = sorted(s)

Upvotes: 2

Related Questions