DEV Community

Karoline Nunes
Karoline Nunes

Posted on

Could u help me ?

I am learning python so i don't have a lot knowledge about the language my code is preseting an error in the end of the code. Could u help me with that problem ?
I will be so glad for that.

-- coding: utf-8 --

"""Data Science from zero

Automatically generated by Colaboratory.

Original file is located at
https://colab.research.google.com/drive/1GHzLZok20tuyOzVbbQc2nNQO6hdlUZgW
"""

users = [
{ "id": 0, "name": "Hero" },
{ "id": 1, "name": "Dunn" },
{ "id": 2, "name": "Sue" },
{ "id": 3, "name": "Chi" },
{ "id": 4, "name": "Thor" },
{ "id": 5, "name": "Clive" },
{ "id": 6, "name": "Hicks" },
{ "id": 7, "name": "Devin" },
{ "id": 8, "name": "Kate" },
{ "id": 9, "name": "Klein" }]

friendships = [(0, 1), (0, 2), (1, 2), (1, 3), (2, 3), (3, 4),
(4, 5), (5, 6), (5, 7), (6, 8), (7, 8), (8, 9)]

for user in users:
user["friends"] = []

from posixpath import join
for i, j in friendships:
users[i]["friends"].append(users[j]) # adiciona i como um amigo de j
users[j]["friends"].append(users[i]) # adiciona j como um amigo de i

def number_of_friends(user):

quantos amigos o usuário tem?

return len(user["friends"]) # tamanho da lista friend_ids
total_connections = sum(number_of_friends(user)
for user in users)

from future import division
num_users = len(users) # tamanho da lista de usuários
avg_connections = total_connections / num_users # 2.4

num_friends_by_id = [(user['id'], number_of_friends(user)) for user in users]

in this part is presenting a error could u help me ?

users_popular = sorted(num_friends_by_id,
key=lambda (user_id, num_friends): num_friends, reverse=True

Top comments (2)

Collapse
 
szabgab profile image
Gabor Szabo • Edited

Hi, I am trying to help so far I got to: the google drive link gives me "You need access" and it would be easier to read your code snippet here on the post if you put them inside triple backticks. That would show the code verbatim. (This is how it is done in Markdown.)

Now one of your comments # quantos amigos o usuário tem? that staretd with a hashmark turned into a title in your post.

An alternative would be to post the code on gist.github.com/ and posting the link, but IMHO learning Markdown would be more valuable.

In addition it would be easier to help if you also included the error message you received.

I see your code has

from __future__ import division
Enter fullscreen mode Exit fullscreen mode

Though in the original post the double underscores were eaten by the lack of the 3 backticks I mentioned at the beginning.

That is only necessary if you use Python 2 but would like to have the division behavior of Python 3. Which version of Python do you use?

Collapse
 
karolinenunes profile image
Karoline Nunes

Hello. Thank u. It was very helpful