Superdooperhero
Superdooperhero

Reputation: 8096

Python regular expression that will find words made out of specific letters. Letters must be used once only

If I have a list of words in the variable words and a list of letters in the variable letters, how can I find all the words that can be made up out of the letters in letters. The letters in letters must only be used once each; but you can list the same letter more than once. All letters in letters must be used. I would like to do this in Python or Excel VBA.

For example:

letters = ['a', 'a', 'a', 'i', 'l', 'r', 's', 't', 'u']
words = ['dummy', 'australia']

Upvotes: 1

Views: 315

Answers (1)

Lev Levitsky
Lev Levitsky

Reputation: 65791

I wouldn't use a regex for this, especially in Python:

>>> [w for w in words if sorted(w) == letters]
['australia']

This assumes that letters is sorted, as in your example.

Upvotes: 3

Related Questions