Home > Back-end >  Find the strings containing a special character from a list of strings in python
Find the strings containing a special character from a list of strings in python

Time:04-03

I want to return all words containing '£', from my list of strings, such as but not limited to :

£s
£re
£
£'
c£po 
d£ 
id£
£million

I have tried this:

import re
sample = ["late £ early £s film","two droids d£ c£po voice" , "hype id£ £  promises " ]

rx = re.compile('\w*£\w*|\W')

lst = set([rx.findall(rx, word) for word in sample])
print(lst)

which unfortunately, gives an error, TypeError: 'str' object cannot be interpreted as an integer.

I have also tried this, which gives loads of empty spaces in my real example, not filtering for the unique words.

lst = [re.findall(r'\w*£\w*|\W', word) for word in sample]

So it should return from the above:

(£,£s,d£,c£po,id£)

thank you.

CodePudding user response:

Try:

import re

sample = [
    "late £ early £s film",
    "two droids d£ c£po voice",
    "hype id£ promises ",
]

x = re.findall(r"\w*£\w*", " ".join(sample))
print(set(x))

Prints:

{'£s', 'c£po', '£', 'd£', 'id£'}
  • Related