python自增列表_字典中的列表自增问题求教

该博客介绍了一个使用Python进行代码统计的程序,通过选择要统计的文件类型(如.py, .c等),遍历指定目录下的文件,计算每个类型文件的数量和有效代码行数。最后,程序会显示统计结果,包括总代码行数和完成进度。" 125189363,8823603,增量训练word2vec模型:加载与保存实践,"['word2vec', '自然语言处理', '人工智能', '模型训练']
摘要由CSDN通过智能技术生成

import easygui as g

import os

codingdict = {}

typedict={}

linecountdict={}

def ChoiceCountType():

'''选择要统计哪些代码文件类型'''

global codingdict

global typedict

global linecountdict

choices = [".py", ".c", ".cpp", ".txt",".cs",".aspx"]  # 设置代码文件选项

usertype = g.multchoicebox(msg="请从下列列表中选择要统计的代码文件类型!", title="代码统计", choices=choices)

#codingdict = dict.fromkeys(usertype, [0, 0])  # 给类型字典赋初值  {类型,[个数,有效代码数]}

typedict=dict.fromkeys(usertype,0)

linecountdict=dict.fromkeys(usertype,0)

dirpath = g.diropenbox()

return dirpath

def calc_code(paths,file_name):

'''统计有效代码行的函数'''

codinglines = 0

with open(paths + "\" + file_name,errors="ignore") as fp:

print('正在分析文件:%s ...' % file_name)

dateline = fp.readlines()

for line in dateline:

if line == "\n":

continue

elif line.startswith("#"):

continue

else:

codinglines+=1

return codinglines

def ChoiceDir(dirpath):

'''用os.walk()遍历计算文件数量和有效代码数分别存进对应列表中'''

global typedict

global linecountdict

for paths, dirs, files in os.walk(dirpath):

if len(files) > 0:  # 如果有文件,就执行下一步的遍历文件列表

for file in files:

ext = os.path.splitext(file)[1]  # 切片出类型,然后判断出类型在不在编程类型文件里

if ext in typedict.keys():  # 如果切出来的类型在字典的KEY中

lines=calc_code(paths,file)   #调用函数算出现在循环到的这个文件有多少有效代码行数

typedict[ext]+=1        #基于这个key给typedict赋值,统计现在这个文件类型的文件数量

linecountdict[ext]+=lines   #基于这个key给linecountdict赋值,统计现在这个类型的文件有多少有效行数

def ShowResult():

msg=""

text=""

title="统计结果"

totallines=0

for i in linecountdict.keys():

for each in typedict.keys():

if i==each:

text += (f"[{i}]类型的文件有{typedict[each]}个,共计有效代码量{linecountdict[i]}行\n")

totallines+=linecountdict[i]

#print(totallines)

msg=f"您目前共累积编写了{totallines}行代码,完成进度:{float(totallines/10000)}%\n离 10 万行代码还差{10000-totallines}行,请继续努力!"

g.textbox(msg, title, text)

openpath = ChoiceCountType()

ChoiceDir(openpath)

ShowResult()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值