How to search for all characters from a string in all dictionaries. ()

First of all, I know that there are many similar messages that I cited below, however, as far as I know, no one answers the problem that I encountered, because all I found is asking how to search for a string in 'dict .values ​​() ', not to search for each character in a string and check if it is in' dict.values ​​() ', and if he found any characters in the string that appear in the' dict characters .values ​​() 'it will return what and how much.

Links to related posts that do not answer the question, but may be useful for some:

How to search if dictionary value contains a specific string with Python

Find dictionary entries whose key matches a substring

How to check if characters in a string are in a dictionary of values?

How to search if dictionary value contains a specific string with Python

This is what I have so far, but doesn't seem to work at all ...

characters = {'small':'abcdefghijklmnopqrstuvwxyz',
              'big':'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 
              'nums':'0123456789',
              'special':"!#$%&()*+,-./:;<=>?@[\]^_{|}~",}

password = 'aAb'

def count(pass_word,char_set):

    num_of_char = 0
    char_list = []

    for char in pass_word:
        if i in char_set.values():
            num_of_char +=1
            char_list += i

    return char_list, num_of_char


#Print result

print(count(password,characters))

The result should be approximately the same:

'a','A','b'
3

I hope you understand what I mean, and if something is unclear, please comment so that I can improve it.

+4
source share
1 answer
def count(password, char_dict):
    sanitized_pass = [char for char in password if any(char in v for v in char_dict.values())]
    return sanitized_pass, len(sanitized_pass)

Here is one way. Another would be to build setall valid c characters and pass this to the function with a password

from itertools import chain


char_set = set(chain.from_iterable(characters.values()))

def count(password, chars):
    sanitized_pass = [char for char in password if char in char_set]
    return sanitized_pass, len(sanitized_pass)
+1
source

Source: https://habr.com/ru/post/1689480/


All Articles