python如何实现统计百分比
前天才接触python
我想统计一个文本中 每个数字出现的百分比
在网上找了一段代码 可以统计出现的次数了
求助下如何统计百分比,谢谢!
以下是统计次数的代码
import sysimport string#import collectionsif len(sys.argv) == 1 or sys.argv[1] in {"-h", "--help"}: print("usage: uniqueword filename_1 filename_2 ... filename_n") sys.exit()else: words = {} # words = collections.defaultdict(int) strip = string.whitespace + string.punctuation + string.digits + "\"'" for filename in sys.argv[1:]: for line in open(filename): for word in line.split(): word = word.strip(strip) if len(word) >= 2: words[word] = words.get(word, 0) + 1 # words[word] += 1 for word in sorted(words): print("'{0}' occurs {1} times".format(word,words[word]))