python字典操作實(shí)例詳解
更新時(shí)間:2017年11月16日 15:38:58 作者:hayden__wang
這篇文章主要為大家詳細(xì)介紹了python字典操作實(shí)例的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了python字典操作實(shí)例的具體代碼,供大家參考,具體內(nèi)容如下
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import turtle
##全局變量##
#詞頻排列顯示個(gè)數(shù)
count = 10
#單詞頻率數(shù)組-作為y軸數(shù)據(jù)
data = []
#單詞數(shù)組-作為x軸數(shù)據(jù)
words = []
#y軸顯示放大倍數(shù)-可以根據(jù)詞頻數(shù)量進(jìn)行調(diào)節(jié)
yScale = 6
#x軸顯示放大倍數(shù)-可以根據(jù)count數(shù)量進(jìn)行調(diào)節(jié)
xScale = 30
################# Turtle Start ####################
#從點(diǎn)(x1,y1)到(x2,y2)繪制線段
def drawLine(t, x1, y1, x2, y2):
t.penup()
t.goto (x1, y1)
t.pendown()
t.goto (x2, y2)
# 在坐標(biāo)(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軸: 坐標(biāo)及描述
for x in range(count):
x=x+1 #向右移一位,為了不畫在原點(diǎn)上
drawText(t, x*xScale-4, -20, (words[x-1]))
drawText(t, x*xScale-4, data[x-1]*yScale+10, data[x-1])
drawBar(t)
#繪制一個(gè)柱體
def drawRectangle(t, x, y):
x = x*xScale
y = y*yScale#放大倍數(shù)顯示
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)
#繪制多個(gè)柱體
def drawBar(t):
for i in range(count):
drawRectangle(t, i+1, data[i])
################# Turtle End ####################
#對(duì)文本的每一行計(jì)算詞頻的函數(shù)
def processLine(line, wordCounts):
#用空格替換標(biāo)點(diǎn)符號(hào)
line = replacePunctuations(line)
#從每一行獲取每個(gè)詞
words = line.split()
for word in words:
if word in wordCounts:
wordCounts[word] += 1
else:
wordCounts[word] = 1
#空格替換標(biāo)點(diǎn)的函數(shù)
def replacePunctuations(line):
for ch in line:
if ch in "~@#$%^&*()_-+=<>?/,.:;{}[]|\'""":
line = line.replace(ch, " ")
return line
def main():
#用戶輸入一個(gè)文件名
filename = input("enter a filename:").strip()
infile = open(filename, "r")
#建立用于計(jì)算詞頻的空字典
wordCounts = {}
for line in infile:
processLine(line.lower(), wordCounts)
#從字典中獲取數(shù)據(jù)對(duì)
pairs = list(wordCounts.items())
#列表中的數(shù)據(jù)對(duì)交換位置,數(shù)據(jù)對(duì)排序
items = [[x,y]for (y,x)in pairs]
items.sort()
#輸出count個(gè)數(shù)詞頻結(jié)果
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()
#根據(jù)詞頻結(jié)果繪制柱狀圖
turtle.title('詞頻結(jié)果柱狀圖')
turtle.setup(900, 750, 0, 0)
t = turtle.Turtle()
t.hideturtle()
t.width(3)
drawGraph(t)
turtle.done()
#調(diào)用main()函數(shù)
if __name__ == '__main__':
main()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Python數(shù)據(jù)結(jié)構(gòu)與算法之字典樹實(shí)現(xiàn)方法示例
- Python數(shù)據(jù)分析中Groupby用法之通過字典或Series進(jìn)行分組的實(shí)例
- Python字典數(shù)據(jù)對(duì)象拆分的簡(jiǎn)單實(shí)現(xiàn)方法
- 關(guān)于Python數(shù)據(jù)結(jié)構(gòu)中字典的心得
- Python實(shí)現(xiàn)嵌套列表及字典并按某一元素去重復(fù)功能示例
- Python3中的列表,元組,字典,字符串相關(guān)知識(shí)小結(jié)
- python嵌套字典比較值與取值的實(shí)現(xiàn)示例
- Python字典,函數(shù),全局變量代碼解析
相關(guān)文章
Django實(shí)現(xiàn)的自定義訪問日志模塊示例
這篇文章主要介紹了Django實(shí)現(xiàn)的自定義訪問日志模塊,結(jié)合具體實(shí)例形式分析了Django針對(duì)日志的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
淺談python函數(shù)調(diào)用返回兩個(gè)或多個(gè)變量的方法
今天小編就為大家分享一篇淺談python函數(shù)調(diào)用返回兩個(gè)或多個(gè)變量的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01
Python3實(shí)現(xiàn)的判斷環(huán)形鏈表算法示例
這篇文章主要介紹了Python3實(shí)現(xiàn)的判斷環(huán)形鏈表算法,涉及Python針對(duì)環(huán)形鏈表的遍歷、判斷相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
詳解Python腳本如何消費(fèi)多個(gè)Kafka?topic
kafka-python庫(kù)是一個(gè)流行的Kafka客戶端庫(kù),本文主要為大家詳細(xì)介紹了如何通過這個(gè)庫(kù)創(chuàng)建一個(gè)Kafka消費(fèi)者,并同時(shí)消費(fèi)多個(gè)Kafka?topic,需要的可以了解下2024-11-11
一文帶你掌握Python內(nèi)置reversed函數(shù)的使用
Python作為一門強(qiáng)大的編程語言,提供了許多內(nèi)置函數(shù)來處理各種數(shù)據(jù)結(jié)構(gòu)和對(duì)象,本文將詳細(xì)探討reversed函數(shù)的用法、示例代碼以及實(shí)際應(yīng)用場(chǎng)景,需要的可以參考下2024-01-01

