user11832421
user11832421

Reputation:

how to apply regex on iteration in python

I have one issue while iterating the regex.

def wordsaraemovalFromPpt():
        pptxListremove = []
        for i in pptxFileList:
            ppt = Presentation(i)
            for j in userInputForRemove: #["Car","car","caR","Cars","ZZCarSzz"]
                for slide in ppt.slides:
                    for shape in slide.shapes:
                        if shape.has_text_frame:
                            pattern = re.findall(j, shape.text, re.IGNORECASE)
                            pptxListremove.extend(pattern)
                print(pptxListremove)
            ppt.save(i)
    wordsaraemovalFromPpt()

Suppose in ppt file i have word like "Car","car","caR","Cars","ZZCarSzz".

but i want only "Car","car","caR",

i dont want "Cars","ZZCarSzz"

Upvotes: 0

Views: 41

Answers (3)

user11832421
user11832421

Reputation:

your_list = ["Car","car","caR","Cars","ZZCarSzz"]
your_list = list(filter(lambda w: w.lower() == "car", your_list))
print(your_list)

It worked for me. Thanks

Upvotes: 0

Ocaso Protal
Ocaso Protal

Reputation: 20237

You can also use a list comprehension for this:

pptxListremove = [x for x in userInputForRemove if x.lower() == "car"]

Upvotes: 1

Amin Guermazi
Amin Guermazi

Reputation: 1722

you don't need regex for that. Just use the filter built-in function

your_list = ["Car","car","caR","Cars","ZZCarSzz"]
your_list = list(filter(lambda w: w.lower() == "car", your_list))
print(your_list)

and output

['Car', 'car', 'caR']

Upvotes: 2

Related Questions