I am retyping the code examples in Joel Grus' "Data Science from Scratch: First Principles with Python." They are written in Python 2.7, but my installation is Python 3.6.0.
The following code block (transcribed from p. 5) generates a syntax error whenever I run it in Python 3.6:
# create a list (user_id, number_of_friends)
num_friends_by_id = [(user["id"], number_of_friends(user))
for user in users]
sorted(num_friends_by_id, # get it sorted
key=lambda (user_id, num_friends): num_friends, # by num_friends
reverse=True) # largest to smallest
Specifically:
key=lambda (user_id, num_friends): num_friends, # by num_friends
^
SyntaxError: invalid syntax
From browsing StackOverflow and elsewhere, I'm guessing that the issue is the attempt to use (user_id, num_friends)
with the lambda; all other examples I've seen seem to only provide a single parameter (if that's the right term), and it's not enclosed in parens. But I can't figure out (a) exactly what's wrong, and (b) how to tweak it so it works in 3.6.
I should note that I'm also confused by how Python is recognizing and doing anything with num_friends
, given that said variable has never previously been defined (but number_of_friends
has). However, since this is not listed in the book's errata, I am assuming there is a difference in the syntax between 2.7 and 3.6 that I'm not understanding.