Python Forum
how do i get rid of next lines in my list()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how do i get rid of next lines in my list()
#1
im taking a coding course and im stuck on an assignment,
fname = input("Enter file name: ")
fh = open(fname)
lst = list()
for line in fh:
    lst.append(line.split( '[' ))
print(lst)
the results give me
Output:
[['But', 'soft', 'what', 'light', 'through', 'yonder', 'window', 'breaks'], ['It', 'is', 'the', 'east', 'and', 'Juliet', 'is', 'the', 'sun'], ['Arise', 'fair', 'sun', 'and', 'kill', 'the', 'envious', 'moon'], ['Who', 'is', 'already', 'sick', 'and', 'pale', 'with', 'grief']]
and i want go get rid of those 3 new lines it starts.

How would anyone get rid of those, thanks for your time!

LINKS https://drive.google.com/open?id=1RUtLX7...-AYuGhm_cq
https://drive.google.com/open?id=1Z9vmGt...Ddc2CbSsne
Reply
#2
Ok, so what I'm getting is that you want to combine it all into one list. All you have to do is re-combine everything into one list like so
newList = []
for subList in list:
    for obj in subList:
        newList.append(obj)
It will iterate through each object of each list and add it to the new list.
Reply
#3
Thanks man, that really helped. Now how would you write code to then combine all of the character into a normal sentence?

This is the character I want to combine.
Output:
['B', 'u', 't', '', 's', 'o', 'f', 't', '', 'w', 'h', 'a', 't', '', 'l', 'i', 'g', 'h', 't', '', 't', 'h', 'r', 'o', 'u', 'g', 'h', '', 'y', 'o', 'n', 'd', 'e', 'r', '', 'w', 'i', 'n', 'd', 'o', 'w', '', 'b', 'r', 'e', 'a', 'k', 's', '', 'I', 't', '', 'i', 's', '', 't', 'h', 'e', '', 'e', 'a', 's', 't', '', 'a', 'n', 'd', '', 'J', 'u', 'l', 'i', 'e', 't', '', 'i', 's', '', 't', 'h', 'e', '', 's', 'u', 'n', '', 'A', 'r', 'i', 's', 'e', '', 'f', 'a', 'i', 'r', '', 's', 'u', 'n', '', 'a', 'n', 'd', '', 'k', 'i', 'l', 'l', '', 't', 'h', 'e', '', 'e', 'n', 'v', 'i', 'o', 'u', 's', '', 'm', 'o', 'o', 'n', '', 'W', 'h', 'o', '', 'i', 's', '', 'a', 'l', 'r', 'e', 'a', 'd', 'y', '', 's', 'i', 'c', 'k', '', 'a', 'n', 'd', '', 'p', 'a', 'l', 'e', '', 'w', 'i', 't', 'h', '', 'g', 'r', 'i', 'e', 'f']
Reply
#4
What have you tried?
Reply
#5
Not much, im new to coding and doing know what i can and can't do. I've tryed adding each word together but that only works for strings so that doesn't work. And I was looking up if there is a function to combine each character without the quotation marks, and there isn't so im stuck.
Reply
#6
If the empty strings had a space, so not really empty, then you iterate through each of them using a for loop and add it to an already created string variable, like with the list code that I showed you. string += character but from the looks of it, the empty strings, are well, empty. So when iterating through all the characters, write an if statement that if the string is empty, add a just a space, else, add the character. Since your new to coding it's best for you to try making the code yourself this time.
Reply
#7
I figured out my problem. I changed my text into a list far to soon. So now I add my list together while it's a string then split it and sort it when it's all together.
a = open('romeo.txt')
y = " "
q = ' '
for x in a:
    y = y+x
q = (y.split())
q.sort()
print(q)
But now my last problem is getting out my duplicate words.

Output:
['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'and', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'is', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'sun', 'the', 'the', 'the', 'through', 'what', 'window', 'with', 'yonder']
I can get rid of the dups by .remove(), but I was wondering if there is a nicer way to do it. .I have tried to think of a nice way to do it like an if statement or something, but I can't think of any way.
Reply
#8
It is crucial that you try to work things out because that's what programming is about: problem solving. Given the list on paper, what steps would you take to get rid of the duplicate words?
Reply
#9
Going off of what ndc85430 said, it often helps to relate to real life. How would you go about doing something if you were a bot and had a certain task to do? Then implement that into the code.
Reply
#10
I did it boys, thanks for all help. Here is what the finished product looks like.
for list in words:
    if final_list.count(list) == 0:
        final_list.append(list)
print(final_list)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 9,133 Aug-10-2020, 11:01 PM
Last Post: medatib531
  25 blank lines in my sorted_fruits output list! raven61 7 5,572 Aug-09-2018, 11:30 PM
Last Post: raven61
  storing lines from stdin in a list bghosh 2 3,741 May-02-2018, 01:12 PM
Last Post: gruntfutuk
  How to list objects on separate lines? Intelligent_Agent0 3 5,013 Jan-10-2018, 05:35 AM
Last Post: Intelligent_Agent0
  printing a list of lines Skaperen 7 5,795 Jul-30-2017, 03:28 AM
Last Post: Skaperen
  Print list items on separate lines with additional text DBS 2 7,057 Jan-11-2017, 02:57 AM
Last Post: DBS

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020