From 3b84a61e3080f46e3e0ac08dedb030c9f7237cf2 Mon Sep 17 00:00:00 2001 From: Dusan Milovanovic Date: Fri, 21 Apr 2017 14:04:22 +0200 Subject: [PATCH] Update introduction.py Updated with: (1) Deleted user[10] because the data about him is not provided and he's not listed in the users list in the book (page 3) (2) A part of the code from the book (page 5) for sorting the users from "most friends" to "least friends". --- code/introduction.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/code/introduction.py b/code/introduction.py index 87ce7435..8c8959ea 100644 --- a/code/introduction.py +++ b/code/introduction.py @@ -20,8 +20,7 @@ { "id": 6, "name": "Hicks" }, { "id": 7, "name": "Devin" }, { "id": 8, "name": "Kate" }, - { "id": 9, "name": "Klein" }, - { "id": 10, "name": "Jen" } + { "id": 9, "name": "Klein" } ] friendships = [(0, 1), (0, 2), (1, 2), (1, 3), (2, 3), (3, 4), @@ -48,6 +47,18 @@ def number_of_friends(user): num_users = len(users) avg_connections = total_connections / num_users # 2.4 +# 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 + +# each pair is (user_id, num_friends) +# [(1, 3), (2, 3), (3, 3), (5, 3), (8, 3), +# (0, 2), (4, 2), (6, 2), (7, 2), (9, 1)] + ################################ # # # DATA SCIENTISTS YOU MAY KNOW #