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/CAPGFgA1c_Kur546VtWo2RLwVoCaPuFPKVLo9ZYgAPYPOB7iLNQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
