Python Forum
Remove special character from list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove special character from list
#1
Hi guys, if I would to put ' symbol in the symbols, it is obvious that I won't be getting the desire outcome.

I would like to preserve the ' symbol on don't isn't and wouldn't. I've tried with endswith method but still got it wrong. Hope you could help on this.

Current outcome
["Examples", "of", "contractions", "include", "dont", "isnt", "and", "wouldnt"]

Desire outcome
["Examples", "of", "contractions", "include", "don’t", "isn’t", "and", "wouldn’t"]


sentence = "Examples of contractions include: don't, isn't, and wouldn't"

list_sentence = sentence.split()
print(list_sentence)

symbols = "{}()[].,:;-*/&|<>=~$123456789?"

results = []

for element in list_sentence:
    temp = ""
    
    for ch in element:
        if ch not in symbols:
            temp += ch
                
    results.append(temp)

print(results)
Reply
#2
I tried the code and it gives the desired output.
Reply
#3
Hi Gribouilis, thank you for replying.

Yeah, because I've removed ' symbol from symbols. I guess my codes won't work for this circumstances.
Reply
#4
Before the loop through ch in element, replace any instance of "n't" with something improbable like 'nzxzt'. Then after that loop, but before you append to results, replace 'nzxzt' with "n't". This can easily be done with the replace method of strings.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to remove extra space from output list? longmen 3 1,758 May-05-2022, 11:04 PM
Last Post: longmen
  Change each character of list into an different characters Angry_bird89 1 2,022 Jun-19-2020, 08:29 AM
Last Post: Angry_bird89
  Exercise list remove duplicates RavCOder 9 5,198 Oct-23-2019, 04:16 PM
Last Post: jefsummers
  Search character from 2d list to 2d list AHK2019 3 2,439 Sep-25-2019, 08:14 PM
Last Post: ichabod801
  Unexpected character after line continuation character joshyb123 5 10,561 Sep-05-2018, 08:08 AM
Last Post: perfringo
  unexpected character after line continuation character error newbie 10 14,487 Aug-09-2018, 06:07 PM
Last Post: nilamo
  How to keep duplicates and remove all other items in list? student8 1 4,914 Oct-28-2017, 05:52 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

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