2017-05-07 17 views
0

내 프로그램 출력을하기 위해 노력하고있어 :파이썬은 문자를 가지고 라인을 시작하고 두 개의 for 루프를 결합합니까?

a. am {1: '2'} 
b. at {1: '2'} 
c. far {1: '1'} 
... 
ff. glad {1: '2'} 
gg. i {1: '2'} 
hh. it {1: '1'} 
... 
ooo. lets {1: '0'} 
ppp. outcome {1: '2'} 

첫 번째 부분은, 문자가, 행 번호이지만은 "Z에 도달하면 다시 알파벳의 시작 루핑, 문자로 표현. "

두 번째 부분 인 단어는 문자열에서 스캔 한 알파벳순 단어입니다.

세 번째 부분은 {단어가 나타나는 횟수 : 나타나는 문장 번호}입니다.

어떤 이유에서든 열거 루프가 올바르게 작동하지 않으면 예외가 발생합니다.

a. am {1: '2'} 
a. at {1: '2'} 
a. far {1: '1'} 
a. glad {1: '2'} 

내 코드는 다음과 같습니다 : 현재이 출력

import string 
from itertools import cycle 
x=range(1) 

... 

letters = list(string.ascii_lowercase) 
m = len(letters) 
for key in sorted(word_counter): 
    order = {len(word_counter[key]): str(sent_num[key]).replace('[','').replace(']','')} 
     #creates a dictionary with the number of times word appears on the left 
     #and the sentence it appears in on the right 
    for i, (let, e) in enumerate(zip(cycle(letters), x)): 
     testing = (let*(i//m+1)) 
     print("{}. {} {}".format(testing, key, order)) 

나는 변경하는 경우 'X = 범위 (1)'그냥 여러 번 다음에 이동하는 것을 이전 행을 반복 무엇에 다음 단어. 누구든지 나를 도울 수 있습니까?

+0

를? 정수? –

+0

word_counter 및 sent_num은 사전입니다. – Ember

답변

1

당신은 주요 목록과 내부 루프, zip 당신의 cycle 객체가 필요하고 계산이 거기에서 가자하지 않습니다`word_counter`은 무엇입니까

... 
for i, (let, key) in enumerate(zip(cycle(letters), sorted(word_counter))): 
    order = {len(word_counter[key]): str(sent_num[key]).replace('[','').replace(']','')} 
    testing = let*(i//m+1) 
    print("{}. {} {}".format(testing, key, order)) 
+0

정말 고마워요! 작품 :) 그들을 결합하는 방법을 알아낼 couldnt – Ember