【问题标题】:python: entropy conditions for characterspython:字符的熵条件
【发布时间】:2020-11-06 19:52:08
【问题描述】:

我希望它根据波兰字母表和使用的符号计算熵:

  • 如果只有大写或小写字母 32
  • 如果它们对于 64 来说既小又大
  • 如果有数字+10
  • 如果有特殊字符+33

不幸的是,这些条件的第二个公式对我不起作用。当我输入“Apple”时,它会弹出 NameError: 名称 'entropia2' 未定义

import math

def entropy_poland(n):
    print("Znaki nie powtarzają się, więc liczymy ze wzoru Hartleya: ")
    if count_upper == True and count_lower == False and count_other == False and count_number == False:
        entropy2 = math.log2(32)
    elif count_upper == False and count_lower == True and count_other == False and count_number == False:
        entropy2 = math.log2(32)
    elif count_upper == False and count_lower == False and count_other == False and count_number == False:
        entropy2 = math.log2(64)
    elif count_upper == True and count_lower == False and count_other == False and count_number == True:
        entropy2 = math.log2(42)
    elif count_upper == False and count_lower == True and count_other == False and count_number == True:
        entropy2 = math.log2(42)
    elif count_upper == False and count_lower == False and count_other == False and count_number == True:
        entropy2 = math.log2(74)
    elif count_upper == False and count_lower == True and count_other == True and count_number == False:
        entropy2 = math.log2(65)
    elif count_upper == True and count_lower == False and count_other == True and count_number == False:
        entropy2 = math.log2(65)
    elif count_upper == False and count_lower == False and count_other == True and count_number == False:
        entropy2 = math.log2(97)
    elif count_upper == True and count_lower == False and count_other == True and count_number == True:
        entropy2 = math.log2(75)
    elif count_upper == False and count_lower == True and count_other == True and count_number == True:
        entropy2 = math.log2(75)
    elif count_upper == False and count_lower == False and count_other == True and count_number == True:
        entropy2 = math.log2(107)
    return entropy2


count_number = False
count_upper = False
count_lower = False
count_other = False

odp = "Reks"

for ascii in odp:
    if chr(32) <= ascii <= chr(47) or chr(58) <= ascii <= chr(64) or chr(91) <= ascii <= chr(96) or chr(
            123) <= ascii <= chr(126):
        count_other = True
    if chr(48) <= ascii <= chr(57):
        count_number = True
    if chr(65) <= ascii <= chr(90):
        count_upper = True
    if chr(97) <= ascii <= chr(122):
        count_lower = True


print(entropy_poland(odp))
````

【问题讨论】:

  • 我无法复制错误。请发布minimal reproducible example。由于您发布的代码从未尝试使用变量entropia2,因此它无法抛出该错误。在else 子句中的大多数地方你使用entropy2 但在一个地方你使用entropia2 似乎很奇怪。
  • 我试图最小化代码,但我忘了改变熵2

标签: python entropy


【解决方案1】:

如果插入行

print(count_upper,count_lower,count_other,count_number)

entropy_poland 定义的开头你会看到它打印出来

True True False False

在抛出错误之前。该布尔组合不是您的子句涵盖的组合之一。有 4 个变量的 16 种布尔组合,但您只为其中的 12 个定义了 entropy2。你需要重新考虑你的逻辑,要么添加一个else 子句,要么如果这个布尔组合实际上是一个错误,则可能引发(然后处理)一个错误。

【讨论】:

    猜你喜欢
    • 2021-12-31
    • 1970-01-01
    • 2014-05-27
    • 2023-03-27
    • 1970-01-01
    • 2015-02-02
    • 2016-02-05
    • 1970-01-01
    • 2015-06-08
    相关资源
    最近更新 更多