【问题标题】:Is there any way to find a word from a list in a string without spaces?有没有办法从没有空格的字符串中的列表中找到一个单词?
【发布时间】:2020-02-21 23:17:12
【问题描述】:

我正在制作一个程序,用户必须创建一个至少 8 个字符长的密码,包含至少一个数字、至少一个小写字母和至少一个大写字母。困难的部分是密码不能包含瑞典语词典中的任何单词。我已将字典中的每个单词都存储在一个文本文件中。无论如何我可以检查密码是否包含我从字典中制作的列表中的单词?

def main():
    print("Write a password with at least 8 characters", 
          "which contains at least 1 digit,", 
          "\nat least 1 uppercase letter, at least one lowercase character", 
          "and at least 1 special character")
    password = input("The password may not contain any word from the dictionary:")

    if checkAllow(password) == True:
        print("\nYour password is allowed")
    else:
        print("\nYour password is not allowed")
        main()

# This function checks if the password is allowed
def checkAllow(password):
    words = open("dictionary.txt", "r")
    wordlist = words.readlines()

    specialChar = ['!', '@', '#', '¤', '£', '$', '%', '€', '&', '/', '{', '(',
                  '[', ')', ']', '=', '}', '+', '?', '"', '¨', '^', '¨', '*',
                    ',', ';', '.', ':', '-', '_', '<', '>', '|', '§', '½']

    if len(password) >= 8 and any(char.isdigit() for char in password):
        if any(char.isupper() for char in password) and any(char.islower() for char in password):
            if any(char in specialChar for char in password):

# Below I try to check if the password contains a word from the dictionary.
                    if any(word in password for word in wordlist) == False:
                       return True
    else:
        return False

【问题讨论】:

  • if substring in longstring: 会告诉您子字符串是否出现在长字符串中的任何位置。
  • 顺便说一句,if boolean_expression == boolean: return boolean 是一个典型的错误
  • 您应该使用上下文管理器来处理文件对象。此外,变量和函数名称应遵循lower_case_with_underscores 样式。
  • 另外,使用set类型而不是list来执行检查,会快很多。

标签: python passwords


【解决方案1】:

您需要从wordlist 中的单词中删除换行符。

wordlist = [word.rstrip() for word in words.readlines()]

那么您的any() 检查应该可以工作。

【讨论】:

    【解决方案2】:

    无论如何我可以检查密码是否包含来自 我用字典制作的列表?

    你可以使用类似的东西:

    with open("dictionary.txt") as f:
        wordlist = [word.rstrip() for word in list(f) if word]
    
    if any(word in password for word in wordlist) == False:
        ...
    

    注意事项:

    • 检查字典中的每个单词会占用大量 CPU,应避免
    • 字典单词末尾包含换行符,这就是不匹配的原因,您可以使用rstrip() 将其删除

    【讨论】:

      【解决方案3】:

      您需要查看字符串中的所有子字符串并检查它们是否在字典中。我会将字典加载到一个集合中以快速查找:

      def load_dictionary(dict_path):
          word_set = set()
          with open(dict_path, 'r') as f:
              for line in f:
                  for word in line.split():
                      word_set.add(word)
          return word_set
      
      
      def check_substrings(string, word_set):
          for i in range(len(string)):
              for j in range(i + 1, len(string)+ 1):
                  if string[i:j] in word_set:
                      return True
          return False
      
      

      如果单词在字典中,则返回 true。

      另外,最好使用with open('dictionary.txt') as f: 访问文件。然后您不必担心关闭文件。更多信息here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-05
        • 1970-01-01
        相关资源
        最近更新 更多