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. * 
            except KeyError:  *# Not sure what this is doing!  *
                occurences[word] = 1  *# or this*


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




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] <javascript:>> 
> 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] <javascript:>.
>> 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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to