【问题标题】:How can you check if there is a word in a password?如何检查密码中是否有单词?
【发布时间】:2021-06-16 07:40:11
【问题描述】:

我目前正在编写密码生成器。它只包含字母。让我们假设它随机生成一个密码,其中包含一个单词。我怎样才能确保不会发生这种情况?有没有为此提供字典的图书馆?

这样就不会生成这样的密码,例如: xl你好

【问题讨论】:

  • 你描述的词是什么?字典词?
  • 有自然语言处理库,它们包括单词列表。
  • 这是否排除了任何带有“I”或“a”的密码?那些是单词……“我”? “他”?
  • if any(word in password for word in list_of_words):
  • 您应该只检查超过某个阈值的单词。

标签: python python-3.x random passwords


【解决方案1】:

好像nltk 就是你要找的东西

import nltk
nltk.download('words')
from nltk.corpus import words

list_of_words = list(filter(lambda x: len(x)!=1, words.words()))  #filtering out single letter alphabets, since all passwords would have at least one alphabet 

def isWordinPassword(password):
    #accepts string password and returns list of words that exist in the password
    words_in_password = list(filter(lambda x: x in password, list_of_words))
    return words_in_password


password = "xsdhello"
isWordinPassword(password)

输出截图:

我使用过 lambda 和 filter 函数。你可以阅读更多关于他们的信息here

【讨论】:

猜你喜欢
  • 2020-05-13
  • 1970-01-01
  • 2017-12-28
  • 2021-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-04
  • 1970-01-01
相关资源
最近更新 更多