我在这段代码上遇到麻烦

时间:2019-10-30 06:59:10

标签: python-3.x

是说语法无效,我不知道这是怎么回事。

这是程序的其余部分 这是完整的代码

    from collections import Counter
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"] = []
    #print(user)

for i,j in friendships:
    users[i]["friends"].append(users[j])
    users[j]["friends"].append(users[i])
    #print(i,j)

def no_of_friends(user):
    return len(user["friends"])

total_connection = sum(no_of_friends(user)for user in users)
#print(total_connection)


no_users = len(user)
#print(no_users)
avg_connection = total_connection/no_users
no_of_friends_byid = [(user["id"],no_of_friends(user))for user in users]
print(no_of_friends_byid)
a = sorted(no_of_friends_byid,
        key = lambda (user_id:num_frd):num_frd,
        reverse=True)

感谢您的任何帮助。

1 个答案:

答案 0 :(得分:0)

from collections import Counter
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"] = []
    #print(user)

for i,j in friendships:
    users[i]["friends"].append(users[j])
    users[j]["friends"].append(users[i])
    #print(i,j)

def no_of_friends(user):
    return len(user["friends"])

total_connection = sum(no_of_friends(user)for user in users)
#print(total_connection)


no_users = len(user)
#print(no_users)
avg_connection = total_connection/no_users
no_of_friends_byid = [(user["id"],no_of_friends(user))for user in users]
print(no_of_friends_byid)
a = sorted(no_of_friends_byid,
        key = lambda (user_id:num_frd):num_frd,
        reverse=True)