请选择 进入手机版 | 继续访问电脑版
楼主: Nicolle
1629 21

【独家发布】Real-World Python [推广有奖]

巨擘

0%

还不是VIP/贵宾

-

TA的文库  其他...

Python(Must-Read Books)

SAS Programming

Must-Read Books

威望
16
论坛币
12402323 个
通用积分
1620.7415
学术水平
3305 点
热心指数
3329 点
信用等级
3095 点
经验
477211 点
帖子
23879
精华
91
在线时间
9878 小时
注册时间
2005-4-23
最后登录
2022-3-6

Nicolle 学生认证  发表于 2020-12-10 07:21:17 |显示全部楼层 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

本帖被以下文库推荐

huhuhuhu 发表于 2020-12-10 09:44:56 |显示全部楼层 |坛友微信交流群
Chapter_4/practice_barchart.py
  1. """Plot barchart of characters in text file."""
  2. import sys
  3. import os
  4. import operator
  5. from collections import Counter
  6. import matplotlib.pyplot as plt

  7. def load_file(infile):
  8.     """Read and return text file as string of lowercase characters."""
  9.     with open(infile) as f:
  10.         text = f.read().lower()
  11.     return text

  12. def main():
  13.     infile = 'lost.txt'
  14.     if not os.path.exists(infile):
  15.         print("File {} not found. Terminating.".format(infile),
  16.               file=sys.stderr)
  17.         sys.exit(1)
  18.       
  19.     text = load_file(infile)
  20.    
  21.     # Make bar chart of characters in text and their frequency.
  22.     char_freq = Counter(text)
  23.     char_freq_sorted = sorted(char_freq.items(),
  24.                               key=operator.itemgetter(1), reverse=True)
  25.     x, y = zip(*char_freq_sorted)  # * unpacks iterable.
  26.     fig, ax = plt.subplots()
  27.     ax.bar(x, y)
  28.     fig.show()

  29. if __name__ == '__main__':
  30.     main()
复制代码

使用道具

sqy 发表于 2020-12-10 11:49:21 |显示全部楼层 |坛友微信交流群
Real-World Python

使用道具

lcpku 学生认证  发表于 2020-12-10 12:06:53 |显示全部楼层 |坛友微信交流群
好的,看一看

使用道具

renshok 发表于 2020-12-10 13:10:27 |显示全部楼层 |坛友微信交流群
XIEXEIFENXIANG

使用道具

HappyAndy_Lo 发表于 2020-12-10 13:42:44 |显示全部楼层 |坛友微信交流群

使用道具

ekscheng 发表于 2020-12-10 15:38:46 |显示全部楼层 |坛友微信交流群

使用道具

dxystata 发表于 2020-12-10 16:25:18 |显示全部楼层 |坛友微信交流群
谢谢分享!

使用道具

break0oo0 发表于 2020-12-10 17:21:14 |显示全部楼层 |坛友微信交流群
谢谢楼主分享

使用道具

xjg 发表于 2020-12-10 17:23:14 |显示全部楼层 |坛友微信交流群

使用道具

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

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-3-29 21:16