python字典操作实例详解
作者:hayden__wang 时间:2021-05-21 08:22:24
本文实例为大家分享了python字典操作实例的具体代码,供大家参考,具体内容如下
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import turtle
##全局变量##
#词频排列显示个数
count = 10
#单词频率数组-作为y轴数据
data = []
#单词数组-作为x轴数据
words = []
#y轴显示放大倍数-可以根据词频数量进行调节
yScale = 6
#x轴显示放大倍数-可以根据count数量进行调节
xScale = 30
################# Turtle Start ####################
#从点(x1,y1)到(x2,y2)绘制线段
def drawLine(t, x1, y1, x2, y2):
t.penup()
t.goto (x1, y1)
t.pendown()
t.goto (x2, y2)
# 在坐标(x,y)处写文字
def drawText(t, x, y, text):
t.penup()
t.goto (x, y)
t.pendown()
t.write(text)
def drawGraph(t):
#绘制x/y轴线
drawLine (t, 0, 0, 360, 0)
drawLine (t, 0, 300, 0, 0)
#x轴: 坐标及描述
for x in range(count):
x=x+1 #向右移一位,为了不画在原点上
drawText(t, x*xScale-4, -20, (words[x-1]))
drawText(t, x*xScale-4, data[x-1]*yScale+10, data[x-1])
drawBar(t)
#绘制一个柱体
def drawRectangle(t, x, y):
x = x*xScale
y = y*yScale#放大倍数显示
drawLine(t, x-5, 0, x-5, y)
drawLine(t, x-5, y, x+5, y)
drawLine(t, x+5, y, x+5, 0)
drawLine(t, x+5, 0, x-5, 0)
#绘制多个柱体
def drawBar(t):
for i in range(count):
drawRectangle(t, i+1, data[i])
################# Turtle End ####################
#对文本的每一行计算词频的函数
def processLine(line, wordCounts):
#用空格替换标点符号
line = replacePunctuations(line)
#从每一行获取每个词
words = line.split()
for word in words:
if word in wordCounts:
wordCounts[word] += 1
else:
wordCounts[word] = 1
#空格替换标点的函数
def replacePunctuations(line):
for ch in line:
if ch in "~@#$%^&*()_-+=<>?/,.:;{}[]|\'""":
line = line.replace(ch, " ")
return line
def main():
#用户输入一个文件名
filename = input("enter a filename:").strip()
infile = open(filename, "r")
#建立用于计算词频的空字典
wordCounts = {}
for line in infile:
processLine(line.lower(), wordCounts)
#从字典中获取数据对
pairs = list(wordCounts.items())
#列表中的数据对交换位置,数据对排序
items = [[x,y]for (y,x)in pairs]
items.sort()
#输出count个数词频结果
for i in range(len(items)-1, len(items)-count-1, -1):
print(items[i][1]+"\t"+str(items[i][0]))
data.append(items[i][0])
words.append(items[i][1])
infile.close()
#根据词频结果绘制柱状图
turtle.title('词频结果柱状图')
turtle.setup(900, 750, 0, 0)
t = turtle.Turtle()
t.hideturtle()
t.width(3)
drawGraph(t)
turtle.done()
#调用main()函数
if __name__ == '__main__':
main()
来源:http://www.cnblogs.com/hayden1106/p/7833438.html
标签:python,字典
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python之字典对象的几种创建方法
2023-04-28 13:15:57
简述Python中的进程、线程、协程
2021-04-07 11:19:02
js 返回 utf-8 字符编码是多少个字节, 函数 fUtf8BoundCheck
2010-07-31 18:59:00
pyqt5打包成exe可执行文件的方法
2022-02-09 11:25:57
![](https://img.aspxhome.com/file/2023/7/87667_0s.png)
php环境配置 php5 MySQL5 apache2 phpmyadmin安装与配置图文教程
2023-11-14 22:08:47
![](https://img.aspxhome.com/file/2023/7/96257_0s.gif)
pycharm + django跨域无提示的解决方法
2021-11-07 21:15:39
![](https://img.aspxhome.com/file/2023/7/101007_0s.png)
asp之让Session永不过期
2011-04-15 10:42:00
javascript闭包的秘密
2008-09-28 20:39:00
显示某日期所在星期的所有日期asp代码
2008-06-01 16:05:00
网页栅格系统研究(3):粒度问题
2008-10-28 19:46:00
![](https://img.aspxhome.com/file/UploadPic/200810/28/2008102819524130s.png)
python调试工具Birdseye的使用教程
2023-08-22 19:18:48
![](https://img.aspxhome.com/file/2023/4/93714_0s.gif)
python实现五子棋程序
2022-05-26 23:24:31
![](https://img.aspxhome.com/file/2023/3/65973_0s.gif)
基于Python实现剪切板实时监控方法解析
2023-05-31 16:01:59
![](https://img.aspxhome.com/file/2023/8/121128_0s.jpg)
详解python中xlrd包的安装与处理Excel表格
2021-10-23 06:06:59
scrapy-redis分布式爬虫的搭建过程(理论篇)
2022-04-25 20:49:25
![](https://img.aspxhome.com/file/2023/5/76105_0s.jpg)
JavaScript设计模式之适配器模式介绍
2023-08-12 06:36:09
别开生面:纯CSS实现相册滑动浏览
2008-06-26 13:24:00
![](https://img.aspxhome.com/file/UploadPic/20086/26/2008626133524696s.jpg)
儿童学习python的一些小技巧
2021-10-14 05:09:40
避免使用滤镜
2009-10-13 20:30:00
jquery密码强度测试工具源码
2009-12-23 19:38:00
![](https://img.aspxhome.com/file/UploadPic/20101/11/2009106204119-12s.png)