循环遍历列表时引用计算后的指数

2024-05-18 19:14:20 发布

您现在位置: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)

错误:

ciphertext=ciphertext + lowercase_letters_indexed[index]
TypeError: Can't convert 'tuple' object to str implicitly

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


Tags: orin列表forindexifitindexed

热门问题