On Thu, Sep 28, 2017, 6:05 AM jettam <[email protected]> wrote:

> This is great, thanks.
>
> *I see in this section you are building the dictionary, assigning both
> keys and values, but I have some questions, see **red*
>         for word in line.upper().split():   *# I get this, you split the
> words into a list*
>             try:
>                 occurences[word] += 1  *# Looks like you are giving the
> dict keys. But I am not sure how you are also inserting values. *
>

This is standard syntax for using a dictionary. You assign a value to a
key. In this particular line, I am using the  x += 1 expression to add 1 to
the current key value in the dictionary. It is the equivalent of doing

occurrences[word] = occurrences[word] + 1


>             except KeyError:  *# Not sure what this is doing!  *
>                 occurences[word] = 1  *# or this*
>

If you try to access a key in a dictionary that does not exist, Python will
raise a KeyError exception. So first I am trying to increment an existing
key (making the assumption that we had already added the word before). If
we have never added that word yet, we catch the error and just start the
new value at 1. This is how you count up each time you see the same word.

I could have avoided the exception by writing this a different way, where
we actually check if the key exists first

if word in occurrences[word]:
    occurrences[word] += 1
else:
    occurrences[word] = 1

It has the same effect but included needing to look up the key once first
to see if it exist. The previous way I had done it is using the "easier to
ask forgiveness than permission" approach.


>
> *Looks like you are sorting the occurrences dict into a descending order
> based on the values?   So h**ow are you telling sorted to look at the
> values fields to sort ? *
> ordered = sorted(occurences, key=occurences.get, reverse=True)
>

sort functions by default will just use each item for the comparisons when
sorting. We don't want it to sort by the keys. So sorted() accepts a key
function that it will call for each item to give it the sort key to use for
comparisons. Since a dictionary has a handy  dict.get(key) method for
getting a value for a key, we can just have it use that. Now it will sort
the keys by getting each value for comparison.

https://wiki.python.org/moin/HowTo/Sorting#Key_Functions


>
>
>
> On Tuesday, September 26, 2017 at 7:56:16 PM UTC-7, Justin Israel wrote:
>
>>
>>
>> On Wed, Sep 27, 2017 at 1:02 PM jettam <[email protected]> wrote:
>>
>>> Thanks for your help Justin.  I would like an example.
>>>
>>
>> Here is an example of the changes I had suggested:
>> https://repl.it/Lfva/1
>>
>> occurences = {}
>> punct = set(["'", "?", ".", "!", ",", "\r\n", "-"])
>> with open(inFile, 'r') as fin
>>     for line in fin:
>>         for p in punct:
>>             line = line.replace(p,"")
>>
>>         for word in line.upper().split():
>>             try:
>>                 occurences[word] += 1
>>             except KeyError:
>>                 occurences[word] = 1
>>
>> ordered = sorted(occurences, key=occurences.get, reverse=True)
>> topThree = ordered[:3]
>> for k in topThree:
>>     v = occurences[k]
>>     print 'the word " %s " occured %s times' % (k,v)
>>
>> ​
>>
>>
>>>
>>> *"It would be better to just build up a dictionary directly within that
>>>> word loop. That way you have a unique mapping of words to their
>>>> occurrences. Then you can use sorted(words.items(), words.get) in order to
>>>> sort the words by their value, in reverse order. That resulting list will
>>>> let you slice off the last three, which will be the (key, val) tuples. You
>>>> will no longer have issues with managing separate key/value lists. Let me
>>>> know if you want the example"*
>>>
>>>
>>>
>>> Regarding the zipping of two lists to make a dictionary. I haven't
>>> noticed any disassociate of key and values in the process. The length of
>>> the list did shrink but only because the zipping process removes
>>> duplicates. So instead of seeing the word AND appear 24 times, in the
>>> zipped dictionary it appeared only once like this {'AND':24}
>>>
>>
>> The zipping process doesn't remove duplicates. Converting your list to a
>> set is what removes duplicates. Both calling sorted() and converting to a
>> set() changes the order of your values so that they no longer map to the
>> original keys. So you end up with this order of random values. Then you can
>> no longer map them back to the exact words since you can have duplicate
>> word count values.
>>
>>
>>>
>>>
>>>
>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Python Programming for Autodesk Maya" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/python_inside_maya/0a706b57-0c97-4930-ae92-7dcb9902fb7c%40googlegroups.com
>>> <https://groups.google.com/d/msgid/python_inside_maya/0a706b57-0c97-4930-ae92-7dcb9902fb7c%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/576e1143-dfce-4fb9-aa4c-5228fc086bae%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/576e1143-dfce-4fb9-aa4c-5228fc086bae%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA0XMQR0U_SOkM_brLLarX3ZmxVVnHRRGtv1mf-rKeU1RQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to