The question is somewhat controversial regarding the reuse of letters from the base line. Or, if they should or should not be repeated, or skip skipped letters. This solution addresses this with a function that includes the reuse parameter:
from collections import Counter def anagram_filter(data, base, reuse=True): if reuse: # all characters in objects in data are in base, count ignored base = set(base) return [d for d in data if not set(d).difference(base)] r = [] cb = Counter(base) for d in data: for k, v in Counter(d).iteritems(): if (k not in cb.keys()) or (v > cb[k]): break else: r.append(d) return r
Using:
>>> anagram_filter(['aba', 'acba', 'caz'], 'abc') ['aba', 'acba'] >>> anagram_filter(['aba', 'acba', 'caz'], 'abc', False) [] >>> anagram_filter(['aba', 'cba', 'caz'], 'abc', False) ['cba']
source share