2016-10-26 62 views
0

對於一個賦值,我必須將一個字符串作爲輸入並將其寫爲一個文件。然後,函數從文件中獲取字符串,並將每個單詞放在字典中,其值是該單詞出現在字符串中的次數。這些單詞將被打印在一個「塔」(類似於一個詞雲)中,每個單詞的大小基於單詞出現在字符串中的次數。如何從函數中訪問字典以用於其他函數?

這是兩個重要的功能:

def word_freq_dict(): # function to count the amount of times a word is in the input string 
    file = open("data_file.txt", 'r') 
    readFile = file.read() #reads file 
    words = readFile.split() #splits string into words, puts each word as an element in a list 
    word_dict = {} # empty dictionary for words to be placed in with the amount of times they appear 
    for i in words: 
     word_dict[i] = word_dict.get(i,0) + 1 # adds items in "words" to a dictionary and amount of times they appear 

    return word_dict 

def word_tower(): 
    t = turtle.Turtle() 
    t.hideturtle() # hides cursor 
    t.up() # moves cursor up 
    t.goto(-200, -200) # starts at the -200,-200 position 
    word_freq_dict() #calls dictionary function 
    for key, value in word_dict.items(): 
     t.write(key, font = ('Arial', value*10, 'normal')) 
     t.up(1.5*len(key)) 

讓我解釋的第二功能。我已經導入了烏龜圖形的塔形成。我試圖做的是將word_freq_dict函數放入word_tower函數中以獲得對字典的訪問權限。原因是因爲該單詞必須打印10倍於其在字符串中出現的次數的大小。光標必須上移1.5倍大小的單詞。

運行後,我得到的錯誤是word_dictword_tower功能,我想是因爲它是一個局部變量沒有定義。我怎樣才能訪問它?

回答

3

調用函數不會自動將任何內容保存到當前名稱空間中。你必須明確地分配它。

word_dict = word_freq_dict() 
2

你可以把word_freq_dict()的返回值到名爲word_dict變量,就像這樣:

而不是

word_freq_dict 

,嘗試

word_dict = word_freq_dict() 
0

您需要將word_dict分配給的結果。您正在返回word_dictword_freq_dict(),但它從未被分配。

+0

很公平,我做了我的答案更簡單,但你的答案顯然是正確的一。 –

0

雖然,正如人們已經指出的,有必要將word_freq_dict()的輸出保存到一個變量中,但不足以讓您的代碼正常工作。你的下一個問題是你的turtle.up()使用,這從您的評論和說法,你不明白:

t.up() # moves cursor up 
t.up(1.5*len(key)) 

這個程序升降機(虛擬)筆落(虛擬)文件,以便不畫線發生,它不會移動光標也不會引用參數。第一個電話有意義(只是調整你的評論),第二個電話可能應該是撥打forward(),而不是旋轉烏龜後,例如,通過left(90)指向頁面。

我看到的其他問題是,您可能希望將烏龜移動到頁面的中心底部,而不是(-200,-200),並且您可能想要打印文本以創建適當的塔。最後,您需要對字典的結果進行排序,以便按照使用頻率的順序排列出來,而不是默認的隨機順序。下面,我已經解決了這些問題,也呈現出defaultdict怎麼能在這種情況下是有益的:

from collections import defaultdict 
from turtle import Turtle, Screen 

def word_freq_dict(file_name): 
    """ Count the amount of times words appear in a string """ 

    file = open(file_name) 
    readFile = file.read() # read file 
    words = readFile.split() # splits string into words, puts each word as an element in a list 
    word_dict = defaultdict(int) # dictionary for words to be placed in with amount of times they appear 

    for word in words: 
     word_dict[word] += 1 # adds item in 'words' to a dictionary and amount of times it appears 

    return word_dict 

def word_tower(turtle, screen, file_name): 

    word_dict = word_freq_dict(file_name) # calls dictionary function 

    turtle.up() # lift pen off page 
    turtle.goto(0, - screen.window_height() // 2) # start at the center bottom of the page 
    turtle.left(90) # point turtle up the page for subsequent forward() calls 

    for key in sorted(word_dict, key=lambda k: word_dict[k], reverse=True): # sort words by frequency 
     value = word_dict[key] * 15 # probably should compute this value based on # words and page height 
     turtle.write(key, font=('Arial', value, 'normal'), align='center') 
     turtle.forward(value) 

yertle = Turtle() 
yertle.hideturtle() # hide turtle image 

screen = Screen() 

word_tower(yertle, screen, 'data_file.txt') 

screen.exitonclick() 

這不是一個完整的程序 - 有需要做更多的錯誤檢查(例如打開時文件),需要做出決定如何處理混合情況以及去掉標點符號和其他調整。

這裏當應用到馬克·吐溫報價是輸出的一個例子:

enter image description here

(See here for the quote and the context.)

相關問題