遍历列表时引用计算出的索引

时间:2019-06-09 18:22:22

标签: python

我试图在迭代时引用计算索引。

lowercase_letters_indexed=`[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f'), (7, 'g'), (8, 'h'), (9, 'i'), (10, 'j'), (11, 'k'), (12, 'l'), (13, 'm'), (14, 'n'), (15, 'o'), (16, 'p'), (17, 'q'), (18, 'r'), (19, 's'), (20, 't'), (21, 'u'), (22, 'v'), (23, 'w'), (24, 'x'), (25, 'y'), (26, 'z')]`
for char in plaintext:
    #if character a grammer character or space ignore it as it is not a letter
    if char in grammer or char==" ":
        ciphertext=ciphertext+char
    elif char in lowercase_letters:
        for (index,letter) in lowercase_letters_indexed:
            if char==letter:
                index=(index + cipher_shift) % 26
                ciphertext=ciphertext + lowercase_letters_indexed[index]
                print(ciphertext)

错误:

  

密文=密文+小写字母索引[index]
      TypeError:无法将“ tuple”对象隐式转换为str

我知道在上面的示例中,串联将不起作用,因为我只是引用元组列表,这应该是列表列表吗?

1 个答案:

答案 0 :(得分:0)

听起来您需要索引到元组中才能找到小写字母:

ciphertext=cipher text + lowercase_letters_indexed[index][1]