CODE:
#!/usr/bin/python #-*-Coding:utf-8-*-' Created on 2014-8-12@author:guaguastd@name:friends_likes_number.py ' ' # Impot loginfrom Login Import facebook_login# Import helper#from Helper import pp# import itemgetter from operator import I temgetter# Import prettytablefrom prettytable import prettytable# access to Facebookfacebook_api = Facebook_login () # get F Riends like through single request#friends_like = Facebook_api.get_object (' Me ', fields= ' Id,name,friends.fields (ID, Name,likes) ') #pp (friends_like) # Get friends like through multi requestfriends = Facebook_api.get_connections ("Me", " Friends ") [' data ']likes = {friend[' name ']: facebook_api.get_connections (friend[' id ')," likes ") [' Data '] for friend In Friends[:20]}print ' likes: ', likes# calculate the number of Likenum_likes_by_friend = {Friend:len (Likes[friend]) For friend in likes}pt = prettytable (field_names=[' friend ', ' Num likes ') pt.align[' friend '], Pt.alig n[' Num likes '] = ' l ', ' R ' [Pt.add_row (NLBF) For nlbf in Sorted (Num_likes_by_friend.items (), Key=itemgetter (1), reverse=true )]print "Number of likes per friend" Print PT
RESULT:
Number of likes per friend+--------------------------+-----------+| Friend | Num likes |+--------------------------+-----------+| Ayelén Basualdo | 25 | | Soofi Cat | 25 | | Brenda Magali | 25 | | Brisa Agustina? | 25 | | Jeniifer Mendez | 25 | | Wanda Bianchi | 25 | | Los Mas Lindos del Mundo | 15 | | Kevin Swaggy | 14 | | Jorge Rodriguez | 11 | | Karen CSR | 8 | | Silvia Dutto | 3 | | Nanu Soria | 3 | | Wucheng | 1 |+--------------------------+-----------+
Python counts the number of Facebook users ' hobbies