2014-10-31 62 views
0

我想制作一个playfair密码,但我无法将我的变量放到正确的位置。追加到python中的嵌套列表中

我有一个函数,它一次编码2个纯文本字母并返回编码的等价物,但它只接受单个字母的2个参数(在字符串中)。我需要帮助让我的列表分开,然后编码这对。

这是我

def function(plaintext): 
    temp_hold = '' 
    encode_out = '' 

    sendout = '' 

    #breaks into pairs of 2 (list within a list) 
    temp_hold = [plaintext[i:i+2] for i in range(0, len(plaintext), 2)] 

    for i in range(len(temp_hold)): 
     for j in range(len(temp_hold)): 
      encode_out = encode_pair(temp_hold[i][j], temp_hold[i][j]) 
    print encode_out 
    # encode pair takes (a,b) and returns its encoded value 

print function("abcd") # should print HSCI 
+0

[在python中附加到嵌套列表](http://stackoverflow.com/questions/13763157/appending-to-a-nested-list-in -python) – lurker 2014-10-31 13:03:05

回答

0

如果你真的有一个嵌套的循环?它不应该是...

for letter1, letter2 in temp_hold: 
    encode_out = encode_pair(letter1, letter2) 
    print encode_out 
+0

谢谢,但如果我有一个句子,而不是abcd,这将如何工作? – xPlasmos 2014-10-31 16:33:30

+0

取决于你的意思是一个句子。空格和标点符号是否需要忽略? – jcfollower 2014-10-31 17:49:32

+0

没有空格和标点符号 – xPlasmos 2014-10-31 18:22:32