Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
def number_of_friends(user):
return len(user["friends"])
total_connections =
sum(number_of_friends(user) for user in users)
Processing Data
num_users = len(users)
# length of the users list
#Total connections
avg_connections = total_connections /
num_users
Finding Mutual Friends
def friends_of_friend_ids_bad(user):
return [foaf["id"] for friend in user["friends"] for foaf in friend["friends"] ]
Map
• Takes an array and a processing function and returns a
new array.
Example>
def process(num):
return num + 2;
list = [2, 3, 4, 5, 6]
map(process, list)
#4, 5, 6, 7, 8
Lambda Functions
• Lambda functions are small anonymous
functions without name.
• They are used when they are supposed to be
used in a certain way.
Example>
Normal Function Lambda Function
#4, 5, 6, 7, 8
Today’s Assignment
• From the given dataset, extract the towns
with the most property crimes.