Python Forum

Full Version: how do i get rid of next lines in my list()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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
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.
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']
What have you tried?
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.
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.
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.
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?
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.
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)
Pages: 1 2