Find word by given characters
duncan smith
duncan at invalid.invalid
Sun Nov 1 13:23:25 EST 2020
On 01/11/2020 13:38, Bischoop wrote:
> On 2020-11-01, Bischoop <Bischoop at vimart.net> wrote:
>> I'm working on a script i which user inputs letters and then a printed
>> words containing those letters. The scripts works however I can't solve
>> one problem , it prints also words in which these letters occur more
>> than once.
>> -------------------
>> Fore example:
>> Letters: at
>> Output: auto, autobahn.
>> -------------------
>>
>> I supposed to not print word: "autobahn" because I've given it only one
>> letter "a".
>>
>
> Problem's solved.
>
> ------------
> for word in words:
> if all(word.count(x) == letters.count(x) for x in letters):
> print(word)
> -------------
>
> Thanks for suggestions, I didn't had to use counter but going to look
> into as it seems useful and interesting.
>
> Thank You
>
But this generates the letters counts for each word. They only need to
be generated once (outside the for loop). And using count requires
iterating over the letters / words for each x in letters (rather than
once). For a large enough collection of words you'd notice the difference.
Duncan
More information about the Python-list
mailing list