我正在尝试使用单词的长度和这些长度的频率作为变量将文本放入垂直直方图中。我可以在水平方向上轻松完成,但在垂直方向上我完全迷失了。 (是的,Python的新手和一般的编程)
我只想使用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 :(得分: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('')