python字典操作实例详解

yipeiwu_com6年前Python基础

本文实例为大家分享了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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

win10环境下配置vscode python开发环境的教程详解

win10环境下配置vscode python开发环境的教程详解

前言 VScode是一个相当优秀的IDE,具备开源、跨平台、模块化、插件丰富、启动时间快、颜值高、可高度定制等等优秀的特质,不愧是微软爸爸的私生子。 所以用VScode来编写Pyth...

selenium+python自动化测试之多窗口切换

selenium+python自动化测试之多窗口切换

在很多页面上都有可点击的链接,点击这些链接会打开一个新的窗口,这时如果要在新打开的窗口中操作页面,就需要先切换到新窗口中,如果不进行切换操作,还是操作的上一个页面窗口 浏览器窗口的切换通...

pytorch之添加BN的实现

pytorch之添加BN的实现

pytorch之添加BN层 批标准化 模型训练并不容易,特别是一些非常复杂的模型,并不能非常好的训练得到收敛的结果,所以对数据增加一些预处理,同时使用批标准化能够得到非常好的收敛结果,这...

Python匿名函数及应用示例

本文实例讲述了Python匿名函数及应用。分享给大家供大家参考,具体如下: lambda关键词能创建?型匿名函数。这种函数得名于省略了def声明函数的标准步骤。 代码如下:...

python编程培训 python培训靠谱吗

python编程培训 python培训靠谱吗

我们在论坛和贴吧上看到无论是老的程序员和新手都想更快的入门和精通python编程,但是市面上众多的书籍让大家无从下手,很多书籍和视频都声称能够快速学习,时间有限,大家哪有精力一个个尝试呢...