help! I'm stuck! | Coursera Community
Coursera Header

help! I'm stuck!


Badge +1
Please I need your help. I'm stuck at this point on an assignment. I need to figure out how to combine these 4 list. I tried extend ant + them but I need to identify the list lines. they are from a file.



also this is my code so far:



not looking for someone to do my work, just want to be able to move on to complete this assignment.

Thanks!

14 replies

Badge
It's unclear what your assignment is: what's the input and what's the expected output ? In particular why do you need to identify the file line number that a particular entry came from and what are you supposed to do with that number with respect to the output ?

If all you're doing is reading in lists of words in a file, and are supposed to combine them into a single datastructure (? 1 single Python list, since your code says that this is a python program ?).

Then what you need to do is to create the empty result Python List and for each line that you read form the file, you must parse each word out of the line and append that word to the output Python List. I'm just guessing what you're trying to do.. I don't get what you're supposed to do with the line number of the entry add it to the end of the word as in 'But-1' 'breaks-1'.. etc. ?

When asking a question you need to be more specific
Badge +1
my assignment is: 8.4 Open the file romeo.txt and read it line by line. 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.
You can download the sample data at http://www.py4e.com/code3/romeo.txt

romeo.txt:
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

I'm supposed to create a list with that data.

and my end result should be:
code:
['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']


as you can see in the end result it's all on line in the dictionary. For some reason when I do my code it comes out as 4 separate lines in the dictionary. If I can get it to recognize as one line then I can sort and remove duplicate words. That's my problem I can't get it to one line, I tried extend and "+" but they create duplicate lines. Thanks.
Badge
OK, it is because you are processing one line at a time in the file. Read a line, split sort, print. For each line in the file. The result: 4 separate lines.

To get just a 'single line' result you have to read all the lines in the file first to put them into 1 dictionary.
After you have read in the entire file you now have all the unique entries in one place.
Sort the entries and print your single line.
(I didn't bother the read the assignment text, I'm just going by what you wrote)
Hope this helps..
Badge
If no one has mentioned debugging techniques to you yet, you can find out what has been happening in your program through the liberal use of print statements.

linenum = 0
for line in fh:
print("this is line #"+(linenum++)+" '"+str(line)+"'")
splitLine = line.split()
print("this is the split line: "+str(splitLine));
splitLine.sort()
print("this is the sorted split line: "+str(splitLine))
print(splitLine)

This is brute force but you would see immediately what your program is doing and probably would answer your question for yourself.

Try it !

p.s. overwriting the file descriptor 'fh' to be the split line from the 'fh' is not a good thing to do, use a separate variable for the split line from 'fh'. I called it 'splitLine' in my example.
Badge +1
Sorry, but you just went over my head. I'm just a beginer in python. First hot do i get it to read all the lines first?
Badge
OK that problem is that you want 1 place to hold all the entries before you sort them.
To do this you create the place to hold the entries
You read the contents from the file into that place
Once you have all the entries, you sort them

I would use a Python set to weed out the duplicates then create a list from the set to sort the entries.

wordSet = set()
fh = open(file)
for line in fh:
splitLine = line.split()
for w in splitLine:
wordSet.add(w)

wordList = list(wordSet)
wordList.sort()
print("sorted list: "+str(wordList))

I haven't run it, but it should do what is required.
I strongly recommend that you try the 'print' method of debugging that I mentioned earlier both in your original example and in this code.
It will show you what it is going on each step of the way.

BTW, I'm new to Python myself, welcome to the club !
Badge +1
Thanks! but that did not work for me> I managed to get it this far. Theproblem only the first line is getting stripped and sorted. using this code:
fname = input("Enter file name: ")
fh = open(fname)

for line in fh:
words = line.rstrip()
#print("this is the split line: "+str(words));
words.sort()
words.extend(fh)
print(words)

Badge
Mmm. That's not the proposed code that I wrote. For laughs just try a straight copy of the code that I wrote and try it. Add some print statements to see what it actually does and why what it does is different from the code that you just posted. If you follow the prints you'll see exactly what it is that puts the separate words all on one line and sorts then which is the sticky part of the problem.
Badge +1
Hey thanks for your help but I figured it out. THANKS!
Badge +1
Has anyone has this problem. My output matches the desired output, but I'm getting an append error. see attachment.

Badge +1
I don't know what am I missing.
Userlevel 5
Badge +3
Hi genxcomp1969

Here is the simplest and most powerful piece of advice I can give you; and what I am about to say is not just beginners advice either, its something I do every single day as a software engineer.

  • When solving a problem, divide the problem into the smallest parts possible. Test those small parts and make sure they are working. Finally, glue the parts together.
So your current problem is to:
  1. open a txt file,
  2. read the lines,
  3. get all the words,
  4. and add them to a list.
  5. Check if the list already contains a word.
  6. If a word is in the list, delete it. (Or, not add it in the first place!!)
  7. Sort the list
So that is 7 smaller problems we need to solve. You already seem to understand problems 1-3 so lets take it from there.

spilt(" ") on a sentence returns a list of words. Thus:

code:
sentence = "The Green Fox"

list_of_words = sentence.split(" ")

print(list_of_words) # ==> ["the", "green", "fox"]


Your current problem is that you do not now how to add to lists. Lets make the problem as simple as possible:

code:
a = ["1", "2"]
b = "3"


How do we add b to a?? Again, I want to stress, notice that we are not talking about reading text files or sorting lists and whatnot. We have reduced the current problem to its simplest form. If you can make the list ["1", "2", "3"] by adding a to b then you will likely know enough to solve the actual problem you are having. I will not tell you how to do this, but I will show you where you can find the awnser: https://www.programiz.com/python-programming/methods/list

----------------------------------------

Your next problem is remove duplicate values. Now, one way to do this is indeed to go through the list and actually delete the values. Another solution is to only add words not in the list in the first place! How can we do that?

Well, once again, lets try solve the simplest possible problem.

code:
my_list = ["1"]
list_2 = ["1", "2", "3"]

# Task (part one) ADD all the values in list_2 to my_list


What if we use a for loop?

code:
for word in list_2:
print(word)
# add word to my_list


If you run this code you will notice that word is "1", then "2", then "3". We are trying to add strings to a list. Notice that this is first problem we looked at! Okay, so if you glue together the two problems you should get something looking like this:

code:
my_list = ["1", "1", "2", "3"]


This isn't quite what we want because "1" appears twice. So how to we only add values we have not seen before? Well, lets break this down into 2 smaller problems:

  1. How do I check if something is already in a list?
  2. How do I do (or not do) something based on a condition?
Answer: Google "if else statements" and well, you can probably just copy question 1 directly into google add the word "python" and voila!
Userlevel 5
Badge +3
I don't know what am I missing.

Two things. Removing specific words like that is a really bad idea. So my first post to learn how can solve that part of the problem correctly.

As for why your code is not currently working. Well, I'm guessing it is because you are printing the values and you have commented out the "append". I'm guessing the grader is looking for that method to be called.
Badge +1
Thanks! all I figured it out....

Reply