'Is word defined by default in python or am I missing something?

Just beginning to learn python with some basic courses and got to my first exercise I can't figure out how the solution works. I've done about 30 minutes of googling around but it's a tough topic to find the right answer through googling and just searching blindly through forums (I think this is because my question centers around the use of "word" in the programming... which brings up a lot of red herrings when trying to search it)

The task: Create a dictionary of all unique words in a string, along with the number of their occurences.

A solution based off of the solution they've given, which works for me, would be as follows:

sample_string="The quick quick brown brown brown fox fox jumps jumps jumps jumps"
list_words=sample_string.split()
dictionary = {}
for word in list_words:
    dictionary[word]=list_words.count(word)
dictionary

My problem is, I can't quite wrap my head around how this is working. Specifically where I fail to understand what's happening is these two lines

for word in list_words:
    dictionary[word]=list_words.count(word)

How does it know what "word" is here? I felt like I was following along everything in my course fine until these two lines. I fully understand how it converts into a list using split and how we've created an empty dictionary, but I have no idea how it knows what 'word' is in the 4th line, and really don't know how the fifth line is working at all, and then it magically spits out exactly what we want to finish!

Any help would be greatly appreciated! I'm also new to these forums so any faux pas please let me know.



Solution 1:[1]

First, you have your string with several words in it.

sample_string="The quick quick brown brown brown fox fox jumps jumps jumps jumps"

Second, use the split() method to separate each word. By leaving the parameter empty, it defaults to splitting at spaces. Since the split method does not modify the original string, create a nee variable (list_words) and store the results in it:

list_words=sample_string.split()

Create an empty dictionary

dictionary = {}

Now, use a for loop to iterate over every element of the dictionary. If you know how a for loop works, you know that it iterates over every element of an array. For example, the string below:

string = "Hello Pinder"
for s in string:
    print(s) 

Will print every character (including spaces)

Output:

H
e
l
l
o

P
i
n
d
e
r

Note that the letter s can be anything you want. It just means that it will be the variable used to iterate over every element. With that said, you can use:

string = "Hello Pinder"
for pinder in string:
    print(pinder) 

But it's always nice to use something a bit more meaningful, such as:

for word in words:

or

for e in elements:

etc...

If you have a dictionary and use a for loop, it will return every key in that dictionary:

fruits = {
    'apple':0.5,
    'banana':1.49
}
for f in fruits:
    print(f)

Output:

apple
banana

If instead of returning the keys, you want to return the values, simply do:

print(fruits[f])

Output:

0.5
1.49

Similarly, you can obtain the same result using methods:

The keys() method returns all keys:

for f in fruits.keys():
    print(f)

Output:

apple
banana

The values() method returns all values:

for f in fruits.values():
    print(f)

Output:

0.5
1.49

If you want to change the value of a key, you can simply do the following. Say, we want to change the price of an apple from 0.5 to 0.6 cents. Add the name of the key and assign the new value.

fruits['apple'] = 0.6
print(fruits)

Output:

{'apple': 0.6, 'banana': 1.49}

So this is exactly what happened in the example you provided. What you have here is: First, iterate over every element in the dictionary:

for word in list_words:

Now, take every element that's being iterated and assign a new value, which in this case is the total number of every occurrence of each word

dictionary[word]=list_words.count(word)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1