Help Please I am Frustrated 😤 😭

Hello guys and girls i am really getting upset from this exercise.
The Desired output is —>
['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']

this is my code

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

for line in fh :
	line = line.rstrip()
	
	if line not in lst :
		lst.append(line)

a = lst[0]
b = lst[1]
c = lst[2]
d = lst[3]
e = a + ' ' + b + ' ' + c + ' ' + ' ' + d
e = e.split()
e.sort()

print(e)

this is my 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’t get rid of the repeated words like ‘and’ , ‘is’ , ‘sun’ , ‘the’
i tried the e.remove()
but the exercise told me not to use this function.

i know there supposed to be a loop but i can not figure it out i tried a lot so please tell me what to do to get rid of the repeated words !

Thanks in advance

Please explain what your function should do, we need more details


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Hello,

I feel you :sweat_smile:

Can you use set()? Python sets docs
Just convert list to set then back to list

You can try using an if statement that compares the last appended element/string to what ever it is that’s next. if they DON’T match, add it in :slight_smile:

One minor problem with using set() is that it is unordered, so you’d have to sort it after. Might as well sort and filter in the same for loop when you are reading the file content.

@Dehna I’m confused by a couple things in this code though (you’re not necessarily wrong; I’m just not sure):

  1. Why are you declaring lst as list() in the top instead of creating an empty list?
  2. Why are you creating 4 variables to capture segments of your lst items?

It looks like you’re returning a list anyway, so why split it up and then join it back together?

@sdc_tk421 If starting point is o[utput from this post, the list with duplicates is already sorted. set() doesn’t change order,does it?

It’s hard to help not knowing what the input is and the point of the function.

Hard coding like this does not seem like the best solution:

Plus why turn a list into a string, split it back into a list, then sort the list, when you could just sort the original list? More info could help here.

Hi @Dehna

at line 6 in your code, if your intention is to remove any and all characters from your strings, the rstrip() function only removes trailing white spaces when no arguments have been given. Instead consider:

for line in fh:
    line = line.translate({ord(i): None for i in ".,!?"})

This creates a for loop that iterates through the given string “.,!?” and removes all specified characters. I’m assuming this is your intention as I think your input is a file containing an excerpt of Act 2, Scene 2 from Romeo and Juliet.

Furthermore, at no point in your code have you specified that duplicates need to be removed. Thus, your returned list will contain more than one occurrence of a given word assuming your input contains more than one occurrence.

If you can give us more information on what your exercise is, this will help everyone trying to assist you

it worked at last oooooooh thanks a lot @Annestezia :heart_eyes:

1 Like

thanks but i did tried the .set() and did it for me thanks a lot for your help :relaxed:

hey @eoja thanks for the feedback … the file i am entering giving me 4 lines with 4 lists so i have to make it 1 combined list so i can continue.

hey @HeyItsShawnie sorry for the missing information about the exercise , and thanks for the feedback and help .
This is the file { 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 have to split words and sort them alphabetically the problem was to get rid of the repeated words as i showed in the post to give me the desired output at the top of the post

Cool! Set might be not the best choice for learning and practicing python though, does all work for you, but it’s good for your nervous system :ok_hand: :grin:

1 Like

if you have another way to solve this with a loop i will be glad for sharing it :relaxed: … and yes my nerves cooled down thanks to you :joy:

since the set is an unordered list it can change the existing order when you convert from a list to a set.

I did test it just now though and the sets seems to be retaining the order of the original list, so maybe it has been changed. That would be cool!

print(sorted(list(set(lst))))

This is the easiest way to get rid of the duplicate words and sort them at the same time. I hope you found it easy too.

1 Like

thanks @kishorekumarj for the help :relaxed::heart:

1 Like

Hi, where can I do those exercises?

hi @djhanlj you can do the exercises at :
py4e.com
lessons :+1: good luck

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.