[英]Limitless count for occurrences in a list
我有一段代碼旨在分析目錄中的多個文件,並根據要求輸出各種詳細信息。 我試圖弄清楚如何實現類似直方圖的系統來顯示此數據。
目前,我的輸出如下所示。
Metric of interest? lines_of_code
lines_of_code 8
lines_of_code 23
當我嘗試達到此結果時。
Metric of interest? lines_of_code
Bin range Count
0-9 1
20-29 1
我將如何去做呢? 從我所看到的,大多數人感覺好像是從collections
導入counter
。
這就是這段代碼的樣子。
elif metric_choice == 'lines_of_code':
for files in os.listdir("."):
if files.endswith(".py"):
file_open = open(files, "r")
file_contents = file_open.readlines()
print("{0:20} {1}".format("lines_of_code", (len(file_contents))))
干杯!
import glob
import collections
counter = collections.Counter()
for py_filename in glob.glob('*.py'):
with open(py_filename) as f:
linecount = sum(1 for line in f)
counter[linecount//10] += 1
for i,n in sorted(counter.items()):
print('{}-{}: {}'.format(i * 10, (i + 1) * 10 - 1, n))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.