On 03/11/2020 14:06, Bischoop wrote: > On 2020-11-03, dn <pythonl...@danceswithmice.info> wrote: >> >> >> The (full) specs are not clear. There's certainly room for >> misunderstanding. I'd be happier if I could 'see' a full spec or >> recognise a practical application, because then we'd be better able to >> discuss facts. Meantime, we try to help with what we have been given... >> >> >> The OP's sample code only realises "conforming word[s]" (good term >> BTW!). The snippet does not report any "count". Similarly, there's no >> facts to avoid ("I assume") an assumption about whether "Letters" may >> include duplication - indeed: whether duplication has meaning or should >> be regarded as user-error. >> > > Let me clarify what I want to do: > We all know Scrabble game. > there's a file with Dictionary containing word in each line, the idea is > to input some letters for example mentioned earlier: att, the script > supposed to find all words in which letters: 'a','t','t' occur and print > these words. It supposed not to print word: 'auto' because there's only > one 't' but words such as: 'toast', 'toasty', 'tolerant' are meeting the > criteria becase we have in these words one 'a' and two 't' as user has > input. > I've checked collections counter but seems to complicated for me at this > stage yet and I'm struggling with applying it. >
>>> from collections import Counter >>> letters = 'att' >>> letter_counts = Counter(letters) >>> word = 'tolerate' >>> wd_counts = Counter(word) >>> for char, cnt in letter_counts.items(): print (cnt == wd_counts[char]) True True >>> word = 'auto' >>> wd_counts = Counter(word) >>> for char, cnt in letter_counts.items(): print (cnt == wd_counts[char]) True False >>> or, equivalent to the above loop, but breaking when the first False is generated and returning the single Boolean that you're after, >>> all(cnt == wd_counts[char] for char, cnt in letter_counts.items()) False >>> There's still a lot of scope for improvement, but possibly not by doing simple things. Duncan -- https://mail.python.org/mailman/listinfo/python-list