【发布时间】:2014-12-24 02:20:37
【问题描述】:
DEFAULT_CONNECTION_LEVEL = 10
# Up to a whole day
UPDATE_FRIENDS_EVERY_N_MINUTES = 360
def find_friends_on_app(friends):
"""
Checks each facebook id to find which facebook friends
use our app
"""
facebook_ids = [friend["id"] for friend in friends]
dt_friends = User.objects.filter(
facebook_id__in=facebook_ids)
friends_on_app = {friend.id: DEFAULT_CONNECTION_LEVEL for friend in
dt_friends}
fb_id_on_app = {friend.facebook_id: "value" for friend in
dt_friends}
friends_not_on_app = \
[{'Name': friend.get("name"),
'id': friend.get("id")} for friend in friends if
fb_id_on_app.get(friend.get("id")) is None]
return friends_not_on_app, friends_on_app
代码获取用户的所有使用我们应用程序的 Facebook 好友,并返回一个字典,其中的键是他们在我们数据库中的 id,值是默认连接级别。
代码还应返回所有未使用我们应用的用户 Facebook 好友。
我使 fb_id_on_app 成为 facebook_id 的键的字典,以便我可以利用 O(1) 查找字典的时间。如果我将它设为一个列表,它将是一个 O(n),其中 n 是列表的大小,以检查值是否在其中。
我确定有更好的方法来做到这一点有什么想法吗?我们也在使用 django 框架。
【问题讨论】:
-
这对codereview.stackexchange.com来说可能是一个更好的问题。
标签: python django facebook dictionary