2011-03-24 3 views
0

단어 길이와 빈도를 변수로 사용하여 세로 막대 그래프에 텍스트를 넣으려고합니다. 나는 그것을 쉽게 수평 적으로 할 수 있지만, 수직으로 나아갈 때 나는 완전히 잃어버린다. (예, 파이썬과 일반적인 프로그래밍 신규)Python의 세로 막대 그래프

가 난 단지 파이썬 3 다음

을 위해 내장 된 모듈을 사용하고자하는 것은 내가이에서 당기는거야 때문에 지금까지 (예를 들어, 텍스트가 제공 한 것입니다 파일) : 여기

import itertools 

text = "This is a sample text for this example to work." 
word_list = [] 
word_seq = [] 

text = text.strip() 

for punc in ".,;:!?'-&[]()" + '"' + '/': 
    text = text.replace(punc, "") 

words = text.lower().split() 

for word in words: 
    word_count = len(word) 
    word_list.append(word_count) 

word_list.sort() 

for key, iter in itertools.groupby(word_list): 
    word_seq.append((key, len(list(iter)))) 
+0

간단한 2D 목록 (리스트 목록)로 그 일을보십시오. –

답변

1

당신은 이동 :

#!/usr/bin/env python 
import fileinput 

freq = {} 
max_freq = 0 

for line in fileinput.input(): 
    length = len(line) 
    freq[length] = freq.get(length, 0) + 1 
    if freq[length] > max_freq: 
     max_freq = freq[length] 

for i in range(max_freq, -1, -1): 
    for length in sorted(freq.keys()): 
     if freq[length] >= i: 
      print('#', end='') 
     else: 
      print(' ', end='') 
    print('') 
+0

어떻게 수치 값으로 축을 인쇄합니까? –

관련 문제