Python 3.x 使用余数帮助查找索引?

Python 3.x 使用余数帮助查找索引?,python-3.x,Python 3.x,有人能解释为什么在下面的代码中需要“%26”吗?如果没有“%26”,结果似乎相同。 代码源 这是因为,它用一种风格的加密技术对信息进行加密。它使用余数来确定用哪个字母替换实字母。因为,它用一种风格的加密技术来加密信息。它使用余数来确定用哪个字母替换实字母。如果原始字符的位置超出范围,则需要%26以避免超出映射列表。如果在最后一个字符之后着陆,则此操作将再次从位置0开始 您发布的代码使用了大量文本搜索,这需要相当长的时间 最好只是“查找”一个字符所具有的pos,将step添加到该字符中,然后查找

有人能解释为什么在下面的代码中需要“%26”吗?如果没有“%26”,结果似乎相同。 代码源


这是因为,它用一种风格的加密技术对信息进行加密。它使用余数来确定用哪个字母替换字母。

因为,它用一种风格的加密技术来加密信息。它使用余数来确定用哪个字母替换字母。

如果原始字符的位置超出范围,则需要
%26
以避免超出映射列表。如果在最后一个字符之后着陆,则此操作将再次从位置0开始


您发布的代码使用了大量文本搜索,这需要相当长的时间

最好只是“查找”一个字符所具有的
pos
,将
step
添加到该字符中,然后查找分配给该总和的字符。您不需要完整的大小写映射:您可以测试输入字符并使用
.upper()
从小写字符创建大写字符

# lowercase and uppercase ascii can be taken from constants out of string module
import string

# create a dict that comntains character -> info, only for lowercase
cryptDict = {ch:pos for pos,ch in  enumerate(string.ascii_lowercase)} 

# add the inverse mapping (pos ->character)    
for k,v in cryptDict.items():
    cryptDict[v] = k

def caesar_encrypt(realText, step):
    outText = []
    cryptText = []

    for letter in realText:
        # get what pos this letter is at
        pos = cryptDict.get(letter,None) # gets None if character not in, f.e. 8

        if pos is not None:
            # you need % 26 here, in case your pos + step goes bigger then 26
            # f.e. z = 25,step = 2 => 27 , you do not have any character thats
            # mapped to 27,so you % 26 and use b which is mapped to 1
            crypt = cryptDict[(pos + step)%26]

            # fix casing if input was uppercase
            if letter.isupper():
                crypt = crypt.upper()

            outText.append(crypt)
        else:
            outText.append(letter) # do not forget unmapped values

    return outText

code = caesar_encrypt('abc', 2)
print(code) # ['c','d','e']
cryptDict
看起来是这样的:

{0: 'a',  1: 'b',  2: 'c',  3: 'd',  4: 'e',  5: 'f',  6: 'g',  7: 'h',  8: 'i', 
 9: 'j', 10: 'k', 11: 'l', 12: 'm', 13: 'n', 14: 'o', 15: 'p', 16: 'q', 17: 'r', 
18: 's', 19: 't', 20: 'u', 21: 'v', 22: 'w', 23: 'x', 24: 'y', 25: 'z', 

'a':  0, 'c':  2, 'b':  1, 'e':  4, 'd':  3, 'g':  6, 'f':  5, 'i':  8, 'h':  7, 
'k': 10, 'j':  9, 'm': 12, 'l': 11, 'o': 14, 'n': 13, 'q': 16, 'p': 15, 's': 18, 
'r': 17, 'u': 20, 't': 19, 'w': 22, 'v': 21, 'y': 24, 'x': 23, 'z': 25}
参考:
-

如果原始字符的位置+
步骤
超出范围,则需要
%26
以避免超出映射列表。如果在最后一个字符之后着陆,则此操作将再次从位置0开始


您发布的代码使用了大量文本搜索,这需要相当长的时间

最好只是“查找”一个字符所具有的
pos
,将
step
添加到该字符中,然后查找分配给该总和的字符。您不需要完整的大小写映射:您可以测试输入字符并使用
.upper()
从小写字符创建大写字符

# lowercase and uppercase ascii can be taken from constants out of string module
import string

# create a dict that comntains character -> info, only for lowercase
cryptDict = {ch:pos for pos,ch in  enumerate(string.ascii_lowercase)} 

# add the inverse mapping (pos ->character)    
for k,v in cryptDict.items():
    cryptDict[v] = k

def caesar_encrypt(realText, step):
    outText = []
    cryptText = []

    for letter in realText:
        # get what pos this letter is at
        pos = cryptDict.get(letter,None) # gets None if character not in, f.e. 8

        if pos is not None:
            # you need % 26 here, in case your pos + step goes bigger then 26
            # f.e. z = 25,step = 2 => 27 , you do not have any character thats
            # mapped to 27,so you % 26 and use b which is mapped to 1
            crypt = cryptDict[(pos + step)%26]

            # fix casing if input was uppercase
            if letter.isupper():
                crypt = crypt.upper()

            outText.append(crypt)
        else:
            outText.append(letter) # do not forget unmapped values

    return outText

code = caesar_encrypt('abc', 2)
print(code) # ['c','d','e']
cryptDict
看起来是这样的:

{0: 'a',  1: 'b',  2: 'c',  3: 'd',  4: 'e',  5: 'f',  6: 'g',  7: 'h',  8: 'i', 
 9: 'j', 10: 'k', 11: 'l', 12: 'm', 13: 'n', 14: 'o', 15: 'p', 16: 'q', 17: 'r', 
18: 's', 19: 't', 20: 'u', 21: 'v', 22: 'w', 23: 'x', 24: 'y', 25: 'z', 

'a':  0, 'c':  2, 'b':  1, 'e':  4, 'd':  3, 'g':  6, 'f':  5, 'i':  8, 'h':  7, 
'k': 10, 'j':  9, 'm': 12, 'l': 11, 'o': 14, 'n': 13, 'q': 16, 'p': 15, 's': 18, 
'r': 17, 'u': 20, 't': 19, 'w': 22, 'v': 21, 'y': 24, 'x': 23, 'z': 25}
参考:
-

尝试在不使用
%26
的情况下执行
caesar_encrypt('z',1)
,使用调试技术可以轻松检测到这一点,可以使用断点或打印。使用包含列表中最后一个字母的消息和一个超出范围的步骤来尝试,看看会发生什么。尝试在不使用
%26
的情况下执行
caesar_encrypt('z',1)
,这是使用调试技术可以很容易检测到的,可以使用断点或打印。尝试使用包含列表中最后一个字母的消息和一个超出范围的步骤,看看会发生什么。