简体   繁体   中英

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.

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
]

Why not use a regular expression?

import re
re.sub(r'\d+', '', 'lo2g4.jpg')
'log.jpg'

Just provide another solution:

old_str = "S11imone22.jpg"
new_str = old_str.translate(str.maketrans("", "", "0123456789"))
print(new_str) # Simone.jpg

I still prefer the re solution, which is faster

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