Jump to content

  • Log In with Google      Sign In   
  • Create Account


JacobChristopher

Member Since 06 Nov 2012
Offline Last Active Jan 27 2013 01:20 PM
-----

Posts I've Made

In Topic: Python 3.3.0 sort() method not working?

08 November 2012 - 08:15 PM

Python 3 has a cool new feature where you can supply a key to the 'sort' member and 'sorted' function. For example, to sort your example as a string you can do:
spam.sort(key=str)

Where the key expects a function that takes one argument and returns a value. A more advanced usage I ran into the other day was to sort a list of dictionaries, I wanted to sort by a key in the dictionary. Example:

[source lang="python"]a=[{'name':'Joe','cash':96},{'name':'Bob','cash':77},{'name':'Jane','cash':3},{'name':'Jill','cash':103}]a.sort(key=lambda x: x['cash'],reverse=True)print(a)[/source]

Since you are learning python I'll explain just a bit more.

Dictionary: Things between {} are a dictionary (key-value pair data structure). Value in a dictionary can be referenced easily with a['key name'] like in line 2.

Named arguments: on line 2 of my code, I use the named arguments 'key' and 'reverse'. reverse simply means I want to sort in reverse order (when True).

Lambda: Lambda functions (in simple to understand terms) are nameless functions that implicitly return a value. Keep in mind, key was expecting a function 'pointer', so you can see that when you use lambda it returns a function pointer. I read that using lambda in this situation has a performance hit compared to using an equivalent function, but that's just something to note rather than overly concern yourself with.


Thanks for this! I don't 100% understand this yet, but I've written it down in my notes. It'll click eventually. That's what I'm starting to realize is happening. I'll understand half of what I'm learning right away, and the other half I'm like "ummmmmmm, I kinda get it but not all the way..." and then all of a sudden when a very similar function is being defined I get an "a-ha!" moment. Then I go back to what I was having trouble with and it's no sweat. As lame as it sounds, this is very exciting to me haha.


So is there a workaround I should learn or should I just skip this and continue on learning?

What you should do, is install the correct version of Python for your tutorial (or find a tutorial that is meant for Python 3.x).

There are enough differences between Python 2.x and 3.x to make your learning process a nightmare if you persist with the current tutorial/Python mismatch.


The inventwithpython.com book says to download Python 3.1. I didn't think that there would be too much of a difference between that and 3.3, but looks like I was wrong! The only 3.1 version they have for download is 3.1.5 so I'll give that a try. Thanks!

In Topic: Python 3.3.0 sort() method not working?

07 November 2012 - 10:57 PM

Thanks for the quick reply. So is there a workaround I should learn or should I just skip this and continue on learning? Being completely new to this, I have no idea how important or not important sort() is/was.

PARTNERS