如何从所有Dictionary.Values()中的字符串中搜索所有字符 [英] How to search for all the characters from a string in all Dictionary.Values()

查看:68
本文介绍了如何从所有Dictionary.Values()中的字符串中搜索所有字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我知道下面列出了许多类似的帖子,但是,据我所知,没有一个答案可以解决我所面临的问题,因为我发现的所有问题都在询问如何搜索字符串在'dict.values()'中搜索,而不是搜索字符串中的每个字符,并检查它是否在'dict.values()'中,以及是否找到了字符串中出现在'dict.字符中的任何字符.values()',它将返回哪个和多少.

First of all I know that out there are many similar posts which I have listed below, however, none to my knowledge answer the problem I'm facing this is because all that I have found are asking how to search for a string in 'dict.values()' and not to search every single character in the string and check whether it is in the 'dict.values()' and if it has found any characters in the string that appear in the characters of 'dict.values()' it will return which and how many.

链接到类似的帖子,这些帖子虽然不能回答问题,但对某些人可能有用:

Links to similar posts which don't answer the question but could be useful to some:

如何搜索字典值是否包含使用Python的某些字符串

查找其关键字与子字符串匹配的字典项

如何使用Python搜索字典值是否包含某些字符串

这是我到目前为止所拥有的,但是似乎根本没有用...

This is what I have so far but don'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))

输出应该类似于:

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

希望您理解我的意思,如果有任何不清楚的地方,请发表评论,以便我改善它.

Hopefully, you understand what I mean and if anything unclear please comment so that I can improve it.

推荐答案

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)

这是一种方式.另一个可能是建立所有可接受的c的set 字符,然后使用密码将其传递给功能

Here's one way. Another would be to build a set of all the acceptable c characters and pass that to the function with the 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)

这篇关于如何从所有Dictionary.Values()中的字符串中搜索所有字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆