How do I access a dictionary from a function to be used in another function?

*爱你&永不变心* 提交于 2019-12-25 04:49:10

问题


For an assignment I have to take a string as an input and write it as a file. Then, a function takes the string from the file and puts each word in a dictionary, with the value being the amount of times that word appears in the string. The words will then be printed in a "tower" (similar to a word cloud) with the size of each word based on the amount of times the word appears in the string.

These are the two important functions:

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

and

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))

Let me explain the second function. I have imported turtle graphics for the tower to be formed. What I tried to do is call the word_freq_dict function into the word_tower function in order to get access to the dictionary. The reason for this is because the word has to be printed 10 times the size of the amount of times it appears in the string. The cursor then must move up 1.5 times the size of the word.

After running, the error that I get is that word_dict was not defined in the word_tower function, which I assume is because it's a local variable. How can I access it?


回答1:


Calling a function doesn't automatically save anything in your current namespace. You have to explicitly assign it.

word_dict = word_freq_dict()



回答2:


You can put the return value of the word_freq_dict() into a variable named word_dict, like so:

Instead of

word_freq_dict

, try

word_dict = word_freq_dict()



回答3:


Although, as folks have pointed out, it is necessary to save the output of word_freq_dict() into a variable, it isn't sufficient to get your code working. Your next issue is your use of turtle.up(), which from your comment and argument, you don't understand:

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

This routine lifts the (virtual) pen off the (virtual) paper so that no line drawing takes place, it doesn't move the cursor nor take an argument. The first call makes sense (just adjust your comment), the second call probably should be a call to forward() instead, after rotating the turtle, e.g. via left(90) to point up the page.

Other issues I see is that you probably want to move the turtle to the center bottom of the page, not (-200, -200), and you probably want to print your text centered to make a proper tower. Finally, you need to sort the results of your dictionary so that the words come out in order of frequency of usage, rather than the default random order. Below, I've addressed these issues and also shown how a defaultdict can be helpful in this situation:

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()

This is not a complete program -- there's more error checking that needs to be done (e.g. when opening the file), decisions need to be made about how to handle mixed case as well as strip punctuation, and other tweaks.

Here's an example of the output when applied to a Mark Twain quote:

(See here for the quote and the context.)




回答4:


You need to assign word_dict to the result of word_freq_dict(). You are returning the word_dict in word_freq_dict(), but it is never assigned.



来源:https://stackoverflow.com/questions/40251960/how-do-i-access-a-dictionary-from-a-function-to-be-used-in-another-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!