美文网首页
我的自学之路(python)004

我的自学之路(python)004

作者: 濃姫 | 来源:发表于2019-03-29 11:20 被阅读0次

疯狂地想做点什么

看到训练营里给的实践题,就特别想写点代码,毕竟用起来才是真的。

统计文本里的英文单词词频

我把思路理了理:用自己的话说,就是需要把出现的英文单词统计个数。继而我得用我所学到的for来遍历一遍文本,还得用到with open 这些来对文件打开或关闭,还有字典、列表的函数功能。

之后我查遍了互联网,最后用python把我的语言翻译出来,做了这道题


with open("mytext", "r", encoding="utf-8") as fd:

    def wordcount(str):

         strl_list = str.replace( '\n', ' ').lower().split(" ")

         count_dict = {}

         for str in strl_list:

              if str in count_dict.keys():

                  count_dict[str] += 1

               else:

                   count_dict[str] =1   

           count_list = sorted(count_dict.items(), key=lambda x: x[1], reverse=True)

       return count_list

print(wordcount(fd))

经过这几天的自学,我终于学会了阅读python代码,至少就像自学英语时,不会的我就google,太长的代码先过滤掉,选择与题目相符的最短最简练的代码入手,这上面的代码是我看到➕总结的最短的代码。

一遍一遍地读,一遍一遍地写

相关文章

网友评论

      本文标题:我的自学之路(python)004

      本文链接:https://www.haomeiwen.com/subject/gbtlbqtx.html