On Sat, Sep 23, 2017 at 3:48 PM jettam <[email protected]> wrote:

> Should I be seeing a ' ' around the value of a dictionary?  Like this {
> 'BANANAS': '12',  'EITHER': '1', }  ?
>
> At the bottom of this code I zipped two lists together to make a
> dictionary. I am now trying to list them in order of reoccurrence but I
> think I am unable to do this because the values don't have ' ' around them?
>
> If this is the problem could someone tell me how I could get  ' ' around
> the values ?
>
> # Result: {'ALREADY': 1,
>  'BANANAS': 12,
>  'BEEN': 3,
>  'EITHER': 1,
>  'GET': 1,
>  'HAVENT': 4,
>  'I': 5,
>  'YOU': 2} #
>
>
> ''' lists the top three occurring words found in this text file '''
>
> inFile = r'E:\ProfessionalDevelopment\python\Introduction to Python
> Scripting in Maya\week4\simpleLine.txt'
> holder=[]     # holds all the words
> occurences=[]      # holds a count of the reoccurring words
> with open(inFile, 'r') as fin:
>     for line in fin:
>         #  removes punctuation
>         punctuation=["?","'","\r\n",".","!","-",","]
>         for p in punctuation:
>
>             line = line.replace(p,"")
>
>         wordList = line.upper().split()
>         #print wordList
>
>
>
>
>         for word in wordList:
>             holder.append(word)
>
>
> for num in holder:
>     occurences.append(holder.count(num))
>
> combine = dict(zip(holder, occurences) )
>
>
Having quotes around an item implies that it is a string. When you do
holder.count(num), the return value is an int, which you append to your
list. If you are trying to sort the keys based on the number of occurrences
then an int is actually the more appropriate value anyways so you shouldn't
want them converted to string.

If you have a dictionary with string keys mapped to int values and you want
to sort the keys by their values, you can do so by using sorted(container,
key=sortFunc)
In this case your container is a dict and your sort function would want to
get the value as the sort key.

combine = dict(...)
sortedByValue = sorted(combine, key=combine.get)

​

The combine.get() method already handles taking a key and giving you a
value, so we can just pass it as the key function. You can also reverse the
order:

sortedByValue = sorted(combine, key=combine.get, reverse=True)

​

As an extra side note, you could simplify your existing code by just
building the dictionary as you go, since really you are just building up
lists to add them to another list, to convert them to a dict later.

occurrences = {}#...for word in wordList:
    # Increment the existing count
    # or create a new entry if needed
    try:
        occurrences[word] += 1
    except KeyError:
        occurrences[word] = 1

​


> --
> 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/16b75e66-a71a-4417-a1fa-a3bc1db88348%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/16b75e66-a71a-4417-a1fa-a3bc1db88348%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/CAPGFgA0TGObnFqyKrf9K-8vTVa_nky%3DEgKVq%2B%2BJUDucmDN%3D_6A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to