【问题标题】:Creating 2D List using for loop in Python3 for vigenere cipher在 Python3 中使用 for 循环为 vigenere 密码创建 2D 列表
【发布时间】:2018-08-11 19:19:31
【问题描述】:

所以,我正在尝试制作一个可以在 python 3 中加密和解密 vigenere 密码的程序。我这样做是为了练习,但我真的在努力创建密码矩阵。如果你不熟悉 vigenere 密码, here's a helpful photo of what I want.

我必须将第一项切换到最后一项的功能是shift,它运行良好。我只需要创建列表,并将字母表的每个值都转移过来。我的代码如下。

import string

alpha = list(string.ascii_lowercase)


def shift(inList):  #Shifts the first item in the list to the end
    first = inList[0]
    inList.pop(0)
    inList.append(first)
    return inList

lastList = alpha
cipher = alpha
for item in alpha:
    print(alpha.index(item))
    cipher = cipher[].append([shift(lastList)])
    #global lastList = cipher[-1]
    print(lastList)
    print(cipher)

我的问题是创建保存 vigenere 密码的二维数组。我似乎无法让它工作。以上是我做的最远的,这个解决方案不会编译。

【问题讨论】:

  • 问题是什么?你的解决方案有什么问题?
  • 我编辑了问题以使其更清楚。

标签: python string python-3.x encryption vigenere


【解决方案1】:

如果您只想创建表格,您可以像这样一次性完成:

for i in range(26):
     left = string.ascii_uppercase[i:]
     right = string.ascii_uppercase[:i]
     print('{}{}'.format(left, right))   

【讨论】:

  • 我编辑了这个问题以防不清楚:我正在尝试生成一个二维数组,这就是我正在努力解决的问题。我正在通过 .lower() 传递所有内容,所以大小写应该不是问题。
【解决方案2】:

我正在自己构建一个,所以这是我创建表格的内容:

count = 0
ext_alph = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
alph_table = []
for y in range(26):
    new_row = []
    for x in range(26):
        new_row.append(ext_alph[x+count])
    count += 1
    alph_table.append(new_row)
for r in alph_table:
    print(r)

我发现您可以将您放入每个新行的内容转移。因为我不想直接从字母字符串的末尾移动到开头继续创建后面的行(例如以 x、y、z、a、b... 开头),所以我只是添加了另一个字母到最后。最后两行只是为了显示表格,顺便说一句。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-12
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    相关资源
    最近更新 更多