Jul-14-2022, 12:04 PM
Hello,
I'm going through an introductory Python book and there's some code for a program that finds the most common word in a text file. Please see below for the code:
1 ) Could someone please explain what is going on in the "for word, count in list(counts.items())" line? I think this is creating the for loop that will iterate through and define what the most common word is (as dictated by the if statement that follows). But "list" hasn't previously been mentioned or defined in the code; can someone please explain what "list" is here?
Relatedly, I know that the items() method returns a "view object" containing the key-value pairs for a dictionary. Is that "view object" something that is only displayed on the terminal or is it something Python can work with? (I take it the latter option is correct but any insight would be appreciated).
2 ) The other five lines of code in the third part are relatively simple but I'm unclear on what exactly is happening or why. For example, bigcount is defined as "None" on the first of these lines. Since it's not subsequently mentioned or modified, won't it always be "None"? And, if so, why is there a need for the other "count > bigcount" condition in the or statement that appears a few lines down? To the extent that I understand the code, it seems to me that the "bigcount = None," "bigword = None," and "if bigcount is None or count > bigcount:" lines might be unnecessary.
3 ) How does Python "know" what the most common word is? I can see (with the exception of the line with "list" in it that I don't understand) how it figures out the largest number of times that each word appears ("bigcount"). But I'm unclear on how exactly it arrives at the word that corresponds to. One of the final lines simply says "bigword = word" but I'm not clear on how that results in the most common word (though I figure the answer is probably in the line with "list" in it that I don't understand).
I anticipate that these are truly novice questions so apologies in advance as I am trying to teach myself. Any insights would be greatly appreciated. Thank you.
I'm going through an introductory Python book and there's some code for a program that finds the most common word in a text file. Please see below for the code:
name = input('Enter file:') handle = open(name, 'r') counts = dict() for line in handle: words = line.split() for word in words: counts[word] = counts.get(word, 0) + 1 bigcount = None bigword = None for word, count in list(counts.items()): if bigcount is None or count > bigcount: bigword = word bigcount = count print(bigword, bigcount)I think that I understand the first two parts: They simply open a file (as input by the user), create an empty dictionary, and loop through the file to count each of the words. But I'm getting stuck on the last part of the code.
1 ) Could someone please explain what is going on in the "for word, count in list(counts.items())" line? I think this is creating the for loop that will iterate through and define what the most common word is (as dictated by the if statement that follows). But "list" hasn't previously been mentioned or defined in the code; can someone please explain what "list" is here?
Relatedly, I know that the items() method returns a "view object" containing the key-value pairs for a dictionary. Is that "view object" something that is only displayed on the terminal or is it something Python can work with? (I take it the latter option is correct but any insight would be appreciated).
2 ) The other five lines of code in the third part are relatively simple but I'm unclear on what exactly is happening or why. For example, bigcount is defined as "None" on the first of these lines. Since it's not subsequently mentioned or modified, won't it always be "None"? And, if so, why is there a need for the other "count > bigcount" condition in the or statement that appears a few lines down? To the extent that I understand the code, it seems to me that the "bigcount = None," "bigword = None," and "if bigcount is None or count > bigcount:" lines might be unnecessary.
3 ) How does Python "know" what the most common word is? I can see (with the exception of the line with "list" in it that I don't understand) how it figures out the largest number of times that each word appears ("bigcount"). But I'm unclear on how exactly it arrives at the word that corresponds to. One of the final lines simply says "bigword = word" but I'm not clear on how that results in the most common word (though I figure the answer is probably in the line with "list" in it that I don't understand).
I anticipate that these are truly novice questions so apologies in advance as I am trying to teach myself. Any insights would be greatly appreciated. Thank you.