Trying to reverse the values of a dictionary.

19 views
Skip to first unread message

jettam

unread,
Sep 26, 2017, 12:04:04 PM9/26/17
to Python Programming for Autodesk Maya
This command sorts the values of my dictionary into a numerical order.  I now would like to reverse that numerical order. I can see there is a command called .reverse() that I should be able to include. 
But I have tried all kinds of ways to include it, I just cant get it to work. Could someone advise of the correct syntax. 


wordFrequencey = { 'ARE': 1, 'ART': 2, 'AS': 8, 'AT': 3, 'ATTEMPT': 2, 'AVERSION': 4, 'AWARE': 1, 'AWARENESS': 1}

order = sorted(wordFrequencey.values())

Marcus Ottosson

unread,
Sep 26, 2017, 12:29:54 PM9/26/17
to python_in...@googlegroups.com

Have a look at the doc for sorted, it’s got an argument for just the occasion.

https://docs.python.org/2/library/functions.html#sorted


--
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 python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/c6b0ff1d-5568-4fbb-b553-9fa0b209b959%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jettam

unread,
Sep 26, 2017, 12:41:24 PM9/26/17
to Python Programming for Autodesk Maya
Thanks. so the syntax is:   order = sorted(wordFrequencey.values(),reverse=True)  

Marcus Ottosson

unread,
Sep 26, 2017, 12:47:31 PM9/26/17
to python_in...@googlegroups.com

For completeness, the .reverse() method you tried is an “in-place” operator. Meaning it does reverse a list, but does so by modifying the list you call it on, rather than return a new list.

It’s a subtle but important thing.

l = [1, 2, 3]
assert l[0] == 1
l.reverse()
assert l[0] == 3
l = l.reverse()
assert l == None

Then there’s reversed too.

l = reversed([1, 2, 3])
l[0]
# ERROR, it's an "iterator"
l = list(l)
assert l[0] == 3

On 26 September 2017 at 17:41, jettam <justin...@gmail.com> wrote:
Thanks. so the syntax is:   order = sorted(wordFrequencey.values(),reverse=True)  

On Tuesday, September 26, 2017 at 9:29:54 AM UTC-7, Marcus Ottosson wrote:

Have a look at the doc for sorted, it’s got an argument for just the occasion.

https://docs.python.org/2/library/functions.html#sorted

On 26 September 2017 at 17:04, jettam <justin...@gmail.com> wrote:
This command sorts the values of my dictionary into a numerical order.  I now would like to reverse that numerical order. I can see there is a command called .reverse() that I should be able to include. 
But I have tried all kinds of ways to include it, I just cant get it to work. Could someone advise of the correct syntax. 


wordFrequencey = { 'ARE': 1, 'ART': 2, 'AS': 8, 'AT': 3, 'ATTEMPT': 2, 'AVERSION': 4, 'AWARE': 1, 'AWARENESS': 1}

order = sorted(wordFrequencey.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 python_inside_maya+unsubscribe@googlegroups.com.

--
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 python_inside_maya+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages