regex - selecting words that only have specific letters in them -
i trying print words match regex provide , contain letters provided in second parameter. regex works perfectly, letter selection not.
import re def search(regex,letters): letters=set(letters) pattern=re.compile(regex) word in content: word=word.strip() if(pattern.findall(word)): if letters & set(word): print(word) #search(r'^(n|u|p|g|o|u|e|b|l){6}$') #search(r'^t(i|a)[a-z]{3}') content=["hello","helps","halts","random"] search(r'^h(e|a)[a-z]{3}','hltsa') #returns: hello,helps,halts
my goal return halts, because matches second parameter.
import re def search(regex,letters): letters=set(letters) pattern=re.compile(regex) word in content: word=word.strip() if(pattern.findall(word)): if set(word) >= letters: print(word) #search(r'^(n|u|p|g|o|u|e|b|l){6}$') #search(r'^t(i|a)[a-z]{3}') content=["hello","helps","halts","random"] search(r'^h(e|a)[a-z]{3}','hltsa') #returns: hello,helps,halts
try this, changed if letters & set(word):
in documentation sets can find this
s.issubset(t) s <= t test whether every element in s in t s.issuperset(t) s >= t test whether every element in t in s
Comments
Post a Comment