简体   繁体   中英

append to a nested list in python

I am trying to make a playfair cipher but I am having trouble getting my variables into the right spot.

I have a function that encodes 2 plaintext letters at a time and return the encoded equivalent but it only accepts 2 args of the single letter(in string). I need help getting seperating my list then encoding the pair.

This is what I have

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

Should you really have a nested loop? Shouldn't it be ...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM