鱼C论坛

 找回密码
 立即注册
查看: 1672|回复: 7

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

[复制链接]
发表于 2020-7-26 18:50:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import easygui as g
  2. import os

  3. totalcount = 0
  4. codingdict = {}


  5. def ChoiceCountType():
  6.     '''选择要统计哪些代码文件类型'''
  7.     global codingdict
  8.     choices = [".py", ".c", ".cpp", ".txt"]  # 设置代码文件选项
  9.     usertype = g.multchoicebox(msg="请从下列列表中选择要统计的代码文件类型!", title="代码统计", choices=choices)
  10.     codingdict = dict.fromkeys(usertype, [0,0])  # 给类型字典赋初值  {类型,[个数,有效代码数]}
  11.     dirpath = g.diropenbox()
  12.     return dirpath


  13. def ChoiceDir(dirpath):
  14.     global totalcount
  15.     for paths, dirs, files in os.walk(dirpath):
  16.         if len(files) > 0:  # 如果有文件,就执行下一步的遍历文件列表
  17.             for file in files:
  18.                 # if file[-3:]==".py":  #如果是py文档,执行接下来的操作 这种方式是死写法,用倒着数判断文件类型
  19.                 ext = os.path.splitext(file)[1]  # 这是活写法,切片出类型,然后判断出类型在不在编程类型文件里
  20.                 if ext in codingdict.keys():  # 如果切出来的类型在字典的KEY中
  21.                     codingdict[ext][0] += 1  # 字典里个数+1
  22.                     filelinecount = 0 #原本是设置了一个变量统计每个文件的有效代码
  23.                     with open(paths + "\" + file, encoding="utf-8",
  24.                               errors="ignore") as fp:  # py文件是UTF-8,TXT是ANSI,单独设置会报错所以忽略报错
  25.                         dateline = fp.readlines()
  26.                     for line in dateline:
  27.                         if line == "\n":
  28.                             continue
  29.                         elif line.startswith("#"):
  30.                             continue
  31.                         else:
  32.                             filelinecount += 1  #现在不应该做变量自增,应该是字典里的代码量自增
  33.                             #codingdict.values()[1]+=1 #?????这个咋不办事啊T,T
  34.                             totalcount += 1
  35.                     print(f"{file}有效代码数是{filelinecount}")
  36.     print(f"总代码量是{totalcount}")



  37. openpath = ChoiceCountType()
  38. ChoiceDir(openpath)
  39. for each in codingdict.keys():
  40.     print(f"共有{each}文件{codingdict[each]}个")
复制代码


问题1.png

老哥们,俩问题求教:
1.为啥运行时会先输出一个[0]?
2.我定义了一个类型统计字典{类型,[个数,有效代码数]},想在for file in files循环中,实现文件个数,有效代码量的自增,这个咋处理都处理不好,求老哥们支支招,写了5小时了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-7-26 18:53:04 | 显示全部楼层
if ext in codingdict.keys():  # 如果切出来的类型在字典的KEY中

这样写,文件数量统计好像也不对。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-26 19:54:24 | 显示全部楼层
1.先输出一个[0]是执行下面这行代码就会输出[0],这个模块我也不太清楚
usertype = g.multchoicebox(msg="请从下列列表中选择要统计的代码文件类型!", title="代码统计", choices=choices)
2.python3中dict.values()返回的不是数组类型,dict.keys()也不是,所以还是用这种  codingdict[ext][1]+=1
  1. dic = {'a':1, 'b':[1,2,3]}
  2. print(dic.values())   # dict_values([1, [1, 2, 3]])
  3. print(type(dic.values()))  # <class 'dict_values'>
  4. print(type(dic.keys()))   # <class 'dict_keys'>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-26 19:56:25 | 显示全部楼层
proer 发表于 2020-7-26 19:54
1.先输出一个[0]是执行下面这行代码就会输出[0],这个模块我也不太清楚
usertype = g.multchoicebox(msg= ...

with open()那里好像也有问题 应该是 +'\\'
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-26 20:34:52 | 显示全部楼层
自顶
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-7-26 22:00:29 | 显示全部楼层
  1. import easygui as g
  2. import os

  3. codingdict = {}
  4. typedict={}
  5. linecountdict={}


  6. def ChoiceCountType():
  7.     '''选择要统计哪些代码文件类型'''
  8.     global codingdict
  9.     global typedict
  10.     global linecountdict

  11.     choices = [".py", ".c", ".cpp", ".txt"]  # 设置代码文件选项
  12.     usertype = g.multchoicebox(msg="请从下列列表中选择要统计的代码文件类型!", title="代码统计", choices=choices)
  13.     #codingdict = dict.fromkeys(usertype, [0, 0])  # 给类型字典赋初值  {类型,[个数,有效代码数]}
  14.     typedict=dict.fromkeys(usertype,0)
  15.     linecountdict=dict.fromkeys(usertype,0)
  16.     dirpath = g.diropenbox()
  17.     return dirpath

  18. def calc_code(file_name):
  19.     '''统计有效代码行的函数'''
  20.     codinglines = 0
  21.     with open(file_name, encoding="utf-8",errors="ignore") as fp:
  22.         print('正在分析文件:%s ...' % file_name)
  23.         dateline = fp.readlines()
  24.         for line in dateline:
  25.             if line == "\n":
  26.                 continue
  27.             elif line.startswith("#"):
  28.                 continue
  29.             else:
  30.                 codinglines+=1
  31.     return codinglines


  32. def ChoiceDir(dirpath):
  33.     '''用os.walk()遍历计算文件数量和有效代码数分别存进对应列表中'''
  34.     global typedict
  35.     global linecountdict

  36.     for paths, dirs, files in os.walk(dirpath):
  37.         if len(files) > 0:  # 如果有文件,就执行下一步的遍历文件列表
  38.             for file in files:
  39.                 ext = os.path.splitext(file)[1]  # 切片出类型,然后判断出类型在不在编程类型文件里
  40.                 if ext in typedict.keys():  # 如果切出来的类型在字典的KEY中
  41.                     lines=calc_code(file)   #调用函数算出现在循环到的这个文件有多少有效代码行数
  42.                     typedict[ext]+=1        #基于这个key给typedict赋值,统计现在这个文件类型的文件数量
  43.                     linecountdict[ext]+=1   #基于这个key给linecountdict赋值,统计现在这个类型的文件有多少有效行数

  44. def ShowResult():
  45.     msg="你现在总共写了XXX代码"
  46.     text=""
  47.     title="统计结果"
  48.     lines=0
  49.     totallines=0

  50.     for i in linecountdict.keys():
  51.         print(f"{i}类型的文件共计有效代码量{linecountdict[i]}行")

  52.     for each in typedict.keys():
  53.         print(f"有{each}类型的文件{typedict[each]}个")





  54. openpath = ChoiceCountType()
  55. ChoiceDir(openpath)
  56. ShowResult()
复制代码


这回我参照小甲鱼课后作业,一个字典搞不定就用两个字典分别存,结果报的错让我更迷茫了。
问题2.png
零基础从入门到放弃。。。。大佬们谁能帮忙瞅瞅哪出的问题。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-26 23:36:58 | 显示全部楼层
  1. import easygui as g
  2. import os

  3. codingdict = {}
  4. typedict={}
  5. linecountdict={}


  6. def ChoiceCountType():
  7.     '''选择要统计哪些代码文件类型'''
  8.     global codingdict
  9.     global typedict
  10.     global linecountdict

  11.     choices = [".py", ".c", ".cpp", ".txt",".cs",".aspx"]  # 设置代码文件选项
  12.     usertype = g.multchoicebox(msg="请从下列列表中选择要统计的代码文件类型!", title="代码统计", choices=choices)
  13.     #codingdict = dict.fromkeys(usertype, [0, 0])  # 给类型字典赋初值  {类型,[个数,有效代码数]}
  14.     typedict=dict.fromkeys(usertype,0)
  15.     linecountdict=dict.fromkeys(usertype,0)
  16.     dirpath = g.diropenbox()
  17.     return dirpath

  18. def calc_code(paths,file_name):
  19.     '''统计有效代码行的函数'''
  20.     codinglines = 0
  21.     with open(paths + "\" + file_name,errors="ignore") as fp:
  22.         print('正在分析文件:%s ...' % file_name)
  23.         dateline = fp.readlines()
  24.         for line in dateline:
  25.             if line == "\n":
  26.                 continue
  27.             elif line.startswith("#"):
  28.                 continue
  29.             else:
  30.                 codinglines+=1
  31.     return codinglines


  32. def ChoiceDir(dirpath):
  33.     '''用os.walk()遍历计算文件数量和有效代码数分别存进对应列表中'''
  34.     global typedict
  35.     global linecountdict

  36.     for paths, dirs, files in os.walk(dirpath):
  37.         if len(files) > 0:  # 如果有文件,就执行下一步的遍历文件列表
  38.             for file in files:
  39.                 ext = os.path.splitext(file)[1]  # 切片出类型,然后判断出类型在不在编程类型文件里
  40.                 if ext in typedict.keys():  # 如果切出来的类型在字典的KEY中
  41.                     lines=calc_code(paths,file)   #调用函数算出现在循环到的这个文件有多少有效代码行数
  42.                     typedict[ext]+=1        #基于这个key给typedict赋值,统计现在这个文件类型的文件数量
  43.                     linecountdict[ext]+=lines   #基于这个key给linecountdict赋值,统计现在这个类型的文件有多少有效行数

  44. def ShowResult():
  45.     msg=""
  46.     text=""
  47.     title="统计结果"
  48.     totallines=0

  49.     for i in linecountdict.keys():
  50.         for each in typedict.keys():
  51.             if i==each:
  52.                 text += (f"[{i}]类型的文件有{typedict[each]}个,共计有效代码量{linecountdict[i]}行\n")
  53.             totallines+=linecountdict[i]
  54.     #print(totallines)
  55.     msg=f"您目前共累积编写了{totallines}行代码,完成进度:{float(totallines/10000)}%\n离 10 万行代码还差{10000-totallines}行,请继续努力!"
  56.     g.textbox(msg, title, text)





  57. openpath = ChoiceCountType()
  58. ChoiceDir(openpath)
  59. ShowResult()


复制代码

微信图片_20200726233509.png
老哥们,用两个字典存可以实现了,我想请教下,用一个带列表的字典
  1. #codingdict = dict.fromkeys(usertype, [0, 0])  # 给类型字典赋初值  {类型,[个数,有效代码数]}
复制代码
然后遍历file时让个数和有效代码数自增有办法实现吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-28 09:58:47 | 显示全部楼层
在线求教
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-24 09:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表