变量控制打印Python的行数

时间:2014-04-12 15:45:41

标签: python

我正在试图弄清楚如何使用变量来控制脚本打印的行数。我想使用输出变量并仅打印用户请求的行数。任何帮助将不胜感激。


import sys, os

print ""
print "Running Script..."
print ""
print "This program analyzes word frequency in a file and"
print "prints a report on the n most frequent words."
print ""

filename = raw_input("File to analyze? ")
if os.path.isfile(filename):
  print "The file", filename, "exists!"
else:
  print "The file", filename, "doesn't exist!"
  sys.exit()
  print ""
output = raw_input("Output analysis of how many words? ")

readfile = open(filename, 'r+')

words = readfile.read().split()
wordcount = {}
for word in words:
  if word in wordcount:
    wordcount[word] += 1
  else:
    wordcount[word] = 1

sortbyfreq = sorted(wordcount,key=wordcount.get,reverse=True)
for word in sortbyfreq:
  print "%-20s %10d" % (word, wordcount[word])

2 个答案:

答案 0 :(得分:1)

只需在最后一个循环中创建一个计数器,它会检查完成的循环次数,并在达到某个数字时中断。

limit = {enter number}
counter = 0
for word in sortbyfreq:
  print "%-20s %10d" % (word, wordcount[word])
  counter += 1
  if counter >= limit:
    break

答案 1 :(得分:1)

字典本质上是无序的,因此在按频率排序后,您无法尝试输出元素。

改为使用collections.Counter

from collections import Counter
sortbyfreq = Counter(words)  # Instead of the wordcount dictionary + for loop.

然后,您可以使用以下方式访问用户定义的最常见元素:

n = int(raw_input('How many?: '))
for item, count in sortbyfreq.most_common(n):
    print "%-20s %10d" % (item, count)