简体   繁体   中英

Remove special character from set of values in Python

I want to remove a special character from a set of string values in Python.

I apply the below-mentioned logic.

I'm getting the exact output I wanted, but can anyone help me with a short and more convenient approach?

job = {'job', 'entrepreneur', 'student', 'services', 'housemaid', 'management', 'self- 
employed','admin.', 'blue-collar', 'technician'}
#print(job)
job_copy = set()
val = ''
for j in job:
  if j.isalnum():
    job_copy.add(j)
  else:
    for char in j:
        if char.isalnum():
            val = val+char
    job_copy.add(val)
    val = ''
print(job_copy)

OUTPUT

{'technician', 'housemaid', 'selfemployed', 'entrepreneur', 'job', 'services', 'admin', 'management', 'student', 'bluecollar'}

Here is a shorter solution:

{re.sub('[^A-Za-z]+', '', s) for s in job}

For each string s in job we substitute any special character (ie not an uppercase or lowercase letter) by an empty character.

Use regex and comprehension:

import re
output = set([re.sub('[^A-ZÜÖÄa-z0-9]+', '', s) for s in job])

To make the code short and easy to follow along, you can simply use regular expression ie re module in Python.
And if you haven't been introduced to regular expression before, I will suggest you read this tutorial by real python .

To capture any non-alphanumeric character in the string and replace it with an empty string ( "" ), one can simply use "\W" expression which is the same as [^a-zA-Z0-9_] or [^\w] in the re.sub() method which can be used to substitute a regex pattern in a string with something else.

The code can be rewritten to this;

import re

job = {'job', 'entrepreneur', 'student', 'services', 'housemaid', 'management', 'self-employed','admin.', 'blue-collar', 'technician'}

job_copy = {re.sub(r'\W', '', j) for j in job}

print(job_copy)

Output

{'technician', 'student', 'management', 'bluecollar', 'job', 'services', 'selfemployed', 'housemaid', 'entrepreneur', 'admin'}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM