2014-10-31 2 views
0

플레이 플레이 암호를 만들려고하고 있지만 올바른 변수에 내 변수를 가져 오는 데 문제가 있습니다.파이썬에서 중첩 목록에 추가

나는 한 번에 2 개의 일반 텍스트를 인코딩하고 인코딩 된 동등 물을 반환하는 함수를 가지고 있지만 한 글자의 2 개의 args 만 허용합니다 (문자열에서). 내 목록을 분리하고 그 쌍을 인코딩하는 데 도움이 필요합니다.

이 내가

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

([파이썬에서 중첩 된 목록에 추가]의 중복 가능성 http://stackoverflow.com/questions/13763157/appending-to-a-nested-list-in -python) – lurker

답변

0

당신이 정말 중첩 루프를 가지고 있어야이 무엇인가? 그것은해야하지 ...

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

감사합니다. 그렇지만 abcd가 아닌 문장이 있다면 어떻게 작동합니까? – xPlasmos

+0

문장의 의미에 따라 다릅니다. 공백과 구두점을 무시해야합니까? – jcfollower

+0

예 : 공백이나 구두점이 없습니다. – xPlasmos