我在一个简单的单词频率计数程序中遇到了全局变量引用问题。我检查了here和here以及python docs的答案。但是,我仍然没有得到全局变量引用的想法。
from collections import Counter
with open('c:/Users/Nick/Downloads/sample_file.txt') as f:
words = f.read().lower().split()
c = Counter(words)
total_words = sum(c.values())
def top_n_words(n):
global c
# c = Counter(words)
top_n = c.most_common(n)
print("Top %d words are:" % n)
print("-" * 20)
for w, c in top_n:
# print("%10s: %-10s" % (w, c))
print("{word:>10s}: {counts:<10d}".format(word=w, counts=c))
def word_appears(w):
# global c
c = Counter(words)
print("The word '{word:s}' appears {time:d} times.".format(word = w, time = c[w]))
top_n_words(12)
print("-" * 20)
print("Total words: %d" % total_words)
print("Total words: {t:d}".format(t=sum(c.values())))
word_appears("history")
top_n_words
函数中,我已声明c
是全局的。我应该在word_appears
函数中声明它是全局的吗?它没有用。c
函数中的print
? top_n_words
,word_appears
的顺序是否会影响最终的打印功能?答案 0 :(得分:1)
将所有代码放在函数中,并仅使用参数使计数器可用。例如:
from collections import Counter
def read_words(file_name):
with open(file_name) as f:
words = f.read().lower().split()
return words
def top_n_words(counter, n):
top_n = counter.most_common(n)
print("Top %d words are:" % n)
print("-" * 20)
for w, c in top_n:
# print("%10s: %-10s" % (w, c))
print("{word:>10s}: {counts:<10d}".format(word=w, counts=c))
def word_appears(counter, w):
print("The word '{word:s}' appears {time:d} times.".format(word=w,
time=counter[w]))
if __name__ == '__main__':
def main(file_name):
words = read_words(file_name)
counter = Counter(words)
top_n_words(counter, 12)
print("-" * 20)
print("Total words: %d" % len(words))
word_appears(counter, "history")
main('words.txt')