Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过for循环(Python)修改后打印出整个词典_Python_Dictionary - Fatal编程技术网

通过for循环(Python)修改后打印出整个词典

通过for循环(Python)修改后打印出整个词典,python,dictionary,Python,Dictionary,我正试图编写一个代码,最终将实现使用凯撒密码加密文本。在这一部分中,我试图创建一个新的字典(对于任何移位),将原始字母映射到移位的字母(其中大写->大写和小写->小写,标点符号、数字和空格保持不变) 在下面的代码中,我首先使用一个字典将所有字母(大写和小写)映射到它们自己,然后根据移位对其进行更改: def buildCoder(shift): """ Returns a dict that can apply a Caesar cipher to a letter. T

我正试图编写一个代码,最终将实现使用凯撒密码加密文本。在这一部分中,我试图创建一个新的字典(对于任何移位),将原始字母映射到移位的字母(其中大写->大写和小写->小写,标点符号、数字和空格保持不变)

在下面的代码中,我首先使用一个字典将所有字母(大写和小写)映射到它们自己,然后根据移位对其进行更改:

def buildCoder(shift):
    """
    Returns a dict that can apply a Caesar cipher to a letter.
    The cipher is defined by the shift value. Ignores non-letter characters
    like punctuation, numbers, and spaces.

    shift: 0 <= int < 26
    returns: dict
    """
    import string
    mydict = {'A':'A','B':'B','C':'C','D':'D','E':'E','F':'F','G':'G','H':'H','I':'I','J':'J','K':'K','L':'L','M':'M','N':'N','O':'O','P':'P','Q':'Q','R':'R','S':'S','T':'T','U':'U','V':'V','W':'W','X':'X','Y':'Y','Z':'Z','a':'a','b':'b','c':'c','d':'d','e':'e','f':'f','g':'g','h':'h','i':'i','j':'j','k':'k','l':'l','m':'m','n':'n','o':'o','p':'p','q':'q','r':'r','s':'s','t':'t','u':'u','v':'v','w':'w','x':'x','y':'y','z':'z'} 
    newdict = {}
    for value in mydict:
        if value in string.ascii_uppercase:
            valIndex = string.ascii_uppercase.find(value)
            newVal = string.ascii_uppercase[((valIndex + shift) % 26)]
            newdict.update({str(value):str(newVal)})
            return newdict
        elif value in string.ascii_lowercase:
            valIndex = string.ascii_lowercase.find(value)
            newVal = string.ascii_lowercase[((valIndex + shift) % 26)]
            newdict.update({str(value):str(newVal)})
            return newdict
    return newdict

如何让程序打印整个移位字典?为什么只打印B的值呢?

你只对第一个字母进行移位,然后返回字典,这时程序退出你的函数。您不需要返回语句:

import string

def buildCoder(shift):
mydict = {'A':'A','B':'B','C':'C','D':'D','E':'E','F':'F','G':'G','H':'H','I':'I','J':'J','K':'K','L':'L','M':'M','N':'N','O':'O','P':'P','Q':'Q','R':'R','S':'S','T':'T','U':'U','V':'V','W':'W','X':'X','Y':'Y','Z':'Z','a':'a','b':'b','c':'c','d':'d','e':'e','f':'f','g':'g','h':'h','i':'i','j':'j','k':'k','l':'l','m':'m','n':'n','o':'o','p':'p','q':'q','r':'r','s':'s','t':'t','u':'u','v':'v','w':'w','x':'x','y':'y','z':'z'} 
for value in mydict:
    if value in string.ascii_uppercase:
        valIndex = string.ascii_uppercase.find(value)
        newVal = string.ascii_uppercase[((valIndex + shift) % 26)]
        newdict[value] = newVal
    elif value in string.ascii_lowercase:
        valIndex = string.ascii_lowercase.find(value)
        newVal = string.ascii_lowercase[((valIndex + shift) % 26)]
        newdict[value] = newVal
return newdict

另外,看看在python中更新dict中的vales是如何工作的,它更容易阅读。

还没有尝试过这个方法,但它应该可以工作。这是回报

def buildCoder(shift):
    """
    Returns a dict that can apply a Caesar cipher to a letter.
    The cipher is defined by the shift value. Ignores non-letter characters
    like punctuation, numbers, and spaces.

    shift: 0 <= int < 26
    returns: dict
    """
    import string
    mydict = {'A':'A','B':'B','C':'C','D':'D','E':'E','F':'F','G':'G','H':'H','I':'I','J':'J','K':'K','L':'L','M':'M','N':'N','O':'O','P':'P','Q':'Q','R':'R','S':'S','T':'T','U':'U','V':'V','W':'W','X':'X','Y':'Y','Z':'Z','a':'a','b':'b','c':'c','d':'d','e':'e','f':'f','g':'g','h':'h','i':'i','j':'j','k':'k','l':'l','m':'m','n':'n','o':'o','p':'p','q':'q','r':'r','s':'s','t':'t','u':'u','v':'v','w':'w','x':'x','y':'y','z':'z'} 
    newdict = {}
    for value in mydict:
        if value in string.ascii_uppercase:
            valIndex = string.ascii_uppercase.find(value)
            newVal = string.ascii_uppercase[((valIndex + shift) % 26)]
            newdict.update({str(value):str(newVal)})
        elif value in string.ascii_lowercase:
            valIndex = string.ascii_lowercase.find(value)
            newVal = string.ascii_lowercase[((valIndex + shift) % 26)]
            newdict.update({str(value):str(newVal)})
    return newdict
def构建编码器(班次):
"""
返回可以对字母应用Caesar密码的dict。
密码由移位值定义。忽略非字母字符
比如标点、数字和空格。
班次:0

更干净

那么为什么它会移动“B”而不是,比如说“A”,甚至“B”?我知道字典是未排序的,但它是如何选择的?我如何让它遍历所有的值?在Python中,字典不包含插入顺序或任何特定的顺序,因为accesing元素是一个键,而不是一个固定的位置。应该是
zip(ascii_lowercase,asci_lowercase[shift%26:][ascii_lowercase[:shift%26])
(对于大写也是如此)或者您将省略很多字符。修复:)谢谢:P实际上利用了zips截断,我不需要对第二个参数说[:shift]
def buildCoder(shift):
    """
    Returns a dict that can apply a Caesar cipher to a letter.
    The cipher is defined by the shift value. Ignores non-letter characters
    like punctuation, numbers, and spaces.

    shift: 0 <= int < 26
    returns: dict
    """
    import string
    mydict = {'A':'A','B':'B','C':'C','D':'D','E':'E','F':'F','G':'G','H':'H','I':'I','J':'J','K':'K','L':'L','M':'M','N':'N','O':'O','P':'P','Q':'Q','R':'R','S':'S','T':'T','U':'U','V':'V','W':'W','X':'X','Y':'Y','Z':'Z','a':'a','b':'b','c':'c','d':'d','e':'e','f':'f','g':'g','h':'h','i':'i','j':'j','k':'k','l':'l','m':'m','n':'n','o':'o','p':'p','q':'q','r':'r','s':'s','t':'t','u':'u','v':'v','w':'w','x':'x','y':'y','z':'z'} 
    newdict = {}
    for value in mydict:
        if value in string.ascii_uppercase:
            valIndex = string.ascii_uppercase.find(value)
            newVal = string.ascii_uppercase[((valIndex + shift) % 26)]
            newdict.update({str(value):str(newVal)})
        elif value in string.ascii_lowercase:
            valIndex = string.ascii_lowercase.find(value)
            newVal = string.ascii_lowercase[((valIndex + shift) % 26)]
            newdict.update({str(value):str(newVal)})
    return newdict
def buildCoder(shift):    
    idx = shift % 26
    mapping = dict(zip(ascii_lowercase,ascii_lowercase[idx:] + ascii_lowercase));
    mapping.update(dict(zip(ascii_uppercase,ascii_uppercase[idx:] + ascii_uppercase)))
    return mapping