【问题标题】:Python - Nested loops causing indexerrorPython - 导致索引错误的嵌套循环
【发布时间】:2015-01-30 22:16:59
【问题描述】:

我正在尝试使用二维网格密码来加密 Python 中的字符串,但我的嵌套循环导致超出范围错误。

这是我的代码:

def encrypt():
    before = str(input("Type a string to encrypt: "))
    columns = int(input("How many table columns would you like: "))
    split = [before[i:i+columns] for i in range(0, len(before), columns)]
    rows = len(split)
    after = []
    for i in range(0, columns):
        for j in range(0,rows):
            after.append(split[j][i])
    print(after)

这是我收到的错误:

Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    encrypt()
  File "E:/Python/cipher.py", line 12, in encrypt
    after.append(split[j][i])
IndexError: string index out of range

【问题讨论】:

  • range(n)range(0, n) 相同。 0 是隐含的
  • 你试过交换索引吗?使用i 遍历列,使用j 遍历每个子字符串。所以你应该使用它们。
  • 你检查过你的数组,看看它的元素是否是你期望的长度?
  • @tdelaney 我有,他们是

标签: python loops nested


【解决方案1】:

出现问题是因为您的输入字符串不能保证是行的倍数。例如,使用 3 列 “要加密的输入字符串”无法加密,因为它会生成一个拆分列表 ['Inp','ut','Str','ing','to','En','cry','pt']。请注意,数组中的最后一个元素只有 2 个元素。

如果你用空格填充你的输入字符串:“Input String to Encrypt” 加密在拆分产生时起作用: ['inp', 'ut', 'str', 'ing', 'to', 'en', 'cry', 'pt']

【讨论】:

  • 是的!谢谢你。这是一个很大的帮助!
【解决方案2】:

问题(尝试打印split)是split中的所有元素不一定都是rows长:

Type a string to encrypt: hello world
How many table columns would you like: 3
['hel', 'lo ', 'wor', 'ld']

你必须决定你想做什么。如果不够长,可能会在最后一个字符串的末尾添加空格。

您可能还想看看enumerate。愉快的黑客攻击。

更新:假设您选择使用 2 列,以及可被 2 整除的字符串长度:

Type a string to encrypt: helo
How many table columns would you like: 2
['he', 'lo']
['h', 'l', 'e', 'o']

似乎有效。哦,我不得不稍微修改一下你的代码,因为输入与你想的不一样:

def encrypt():
    before = raw_input("Type a string to encrypt: ")
    columns = int(raw_input("How many table columns would you like: "))
    split = [before[i:i+columns] for i in range(0, len(before), columns)]
    rows = len(split)
    after = []
    for i in range(0, columns):
        for j in range(0,rows):
            after.append(split[j][i])
    print(after)

更新 2:如果您想使用空格填充输入,只需添加以下行:

before += " " * (columns - len(before) % columns)

你最终会得到这个代码:

def encrypt():
    before = raw_input("Type a string to encrypt: ")
    columns = int(raw_input("How many table columns would you like: "))
    before += " " * (columns - len(before) % columns)
    split = [before[i:i+columns] for i in range(0, len(before), columns)]
    rows = len(split)
    after = []
    for i in range(0, columns):
        for j in range(0,rows):
            after.append(split[j][i])
    print ''.join(after)

示例输出:

Type a string to encrypt: hello world
How many table columns would you like: 4
hore llwdlo 

【讨论】:

  • 这不是问题,抱歉。即使行是偶数,我仍然会收到错误消息。编辑:抱歉,我把 i 和 j 弄混了,结果证明这是问题所在。非常感谢!
  • 对我有用,经过小的修改(见更新)
【解决方案3】:

错误是由于行不保证是规则的,并且字符串长度始终不能被列数整除。

(我没有足够的声望来发布图片,抱歉)

http://i.stack.imgur.com/kaJJo.png

第一个网格不起作用,因为在第二个“!”之后有 4 个空格我们无法访问。第二个网格将起作用。

【讨论】:

    猜你喜欢
    • 2019-11-17
    • 2019-09-15
    • 1970-01-01
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多