Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove characters from list of strings using comprehension

I would like to know how to remove certain characters from a list of strings.
In this case, I am trying to remove numbers of type str using list comprehension.

numbers = [str(i) for i in range(10)]
imgs_paths = [os.path.join(input_folder, f) for f in os.listdir(input_folder) if f.endswith('.jpg')]
foo_imgs_paths = [[e.replace(c, "") for c in e if c not in numbers] for e in imgs_paths]

The code above does not work, because it returns completely empty lists.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Option 1

If I understand your question right, a function might simplify it more than nested comprehension.

"doj394no.jpg".replace("0","").replace("1","")... # "dojno.jpg"

If you have your list of files and list of characters to remove:

files = [...]
numbers = "01234556789"

def remove_chars(original, chars_to_remove):
    for char in chars_to_remove:
        original = original.replace(char, "")
    return original

new_files = [remove_chars(file, numbers) for file in files]

Option 2

If you really want to use comprehensions, you can use them to filter letters out without replace:

numbers = "0123456789"
filename = "log234.txt"
[char for char in filename if char not in numbers] # ["l","o","g",".","t","x","t"]
# To return it to a string:
"".join([char for char in filename if char not in numbers]) # "log.txt"

In your case, it would be like so:

numbers = [str(i) for i in range(10)]
imgs_paths = [os.path.join(input_folder, f) for f in os.listdir(input_folder) if f.endswith('.jpg')]
foo_imgs_paths = [
    "".join(char for char in img_path if char not in numbers)
    for img_path in img_paths
]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading