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












0















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']]









share|improve this question




















  • 2





    use extend instead of append.

    – Julien
    Jan 1 at 23:05











  • For this particular assignment I had strict things I could and couldn't do, extend was one of those

    – Jacob Gliddon WD
    Jan 2 at 0:00













  • Actually extend would not help at all here. The logic error comes from appending the wrong thing.

    – tripleee
    Jan 2 at 0:05
















0















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']]









share|improve this question




















  • 2





    use extend instead of append.

    – Julien
    Jan 1 at 23:05











  • For this particular assignment I had strict things I could and couldn't do, extend was one of those

    – Jacob Gliddon WD
    Jan 2 at 0:00













  • Actually extend would not help at all here. The logic error comes from appending the wrong thing.

    – tripleee
    Jan 2 at 0:05














0












0








0








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']]









share|improve this question
















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']]






python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 23:20









bahman parsamanesh

1,543519




1,543519










asked Jan 1 at 23:00









Jacob Gliddon WDJacob Gliddon WD

32




32








  • 2





    use extend instead of append.

    – Julien
    Jan 1 at 23:05











  • For this particular assignment I had strict things I could and couldn't do, extend was one of those

    – Jacob Gliddon WD
    Jan 2 at 0:00













  • Actually extend would not help at all here. The logic error comes from appending the wrong thing.

    – tripleee
    Jan 2 at 0:05














  • 2





    use extend instead of append.

    – Julien
    Jan 1 at 23:05











  • For this particular assignment I had strict things I could and couldn't do, extend was one of those

    – Jacob Gliddon WD
    Jan 2 at 0:00













  • Actually extend would not help at all here. The logic error comes from appending the wrong thing.

    – tripleee
    Jan 2 at 0:05








2




2





use extend instead of append.

– Julien
Jan 1 at 23:05





use extend instead of append.

– Julien
Jan 1 at 23:05













For this particular assignment I had strict things I could and couldn't do, extend was one of those

– Jacob Gliddon WD
Jan 2 at 0:00







For this particular assignment I had strict things I could and couldn't do, extend was one of those

– Jacob Gliddon WD
Jan 2 at 0:00















Actually extend would not help at all here. The logic error comes from appending the wrong thing.

– tripleee
Jan 2 at 0:05





Actually extend would not help at all here. The logic error comes from appending the wrong thing.

– tripleee
Jan 2 at 0:05












2 Answers
2






active

oldest

votes


















1














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:]).






share|improve this answer


























  • The way the file was opened had to remain the same due to the strictness of the assignment, however, it worked and I thank you it helped me understand the statements a lot more

    – Jacob Gliddon WD
    Jan 2 at 0:02



















2














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)





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999602%2fcant-get-the-lines-to-join-up-as-one-list-confused%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    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:]).






    share|improve this answer


























    • The way the file was opened had to remain the same due to the strictness of the assignment, however, it worked and I thank you it helped me understand the statements a lot more

      – Jacob Gliddon WD
      Jan 2 at 0:02
















    1














    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:]).






    share|improve this answer


























    • The way the file was opened had to remain the same due to the strictness of the assignment, however, it worked and I thank you it helped me understand the statements a lot more

      – Jacob Gliddon WD
      Jan 2 at 0:02














    1












    1








    1







    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:]).






    share|improve this answer















    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:]).







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 1 at 23:22

























    answered Jan 1 at 23:15









    tripleeetripleee

    93.4k13130184




    93.4k13130184













    • The way the file was opened had to remain the same due to the strictness of the assignment, however, it worked and I thank you it helped me understand the statements a lot more

      – Jacob Gliddon WD
      Jan 2 at 0:02



















    • The way the file was opened had to remain the same due to the strictness of the assignment, however, it worked and I thank you it helped me understand the statements a lot more

      – Jacob Gliddon WD
      Jan 2 at 0:02

















    The way the file was opened had to remain the same due to the strictness of the assignment, however, it worked and I thank you it helped me understand the statements a lot more

    – Jacob Gliddon WD
    Jan 2 at 0:02





    The way the file was opened had to remain the same due to the strictness of the assignment, however, it worked and I thank you it helped me understand the statements a lot more

    – Jacob Gliddon WD
    Jan 2 at 0:02













    2














    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)





    share|improve this answer




























      2














      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)





      share|improve this answer


























        2












        2








        2







        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)





        share|improve this answer













        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)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 1 at 23:09









        PrimusaPrimusa

        7,8261730




        7,8261730






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999602%2fcant-get-the-lines-to-join-up-as-one-list-confused%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Mossoró

            Error while reading .h5 file using the rhdf5 package in R

            Pushsharp Apns notification error: 'InvalidToken'