2012-07-23 40 views
0

這是中的代碼的副本挖掘社交網絡的書。python和redis,語法不清

我是這方面的新人,也是redis。我想了解$在這方面的含義。還打印%s,這是什麼意思?

這是(從https://github.com/ptwobrussell/Mining-the-Social-Web):下面的源代碼

import sys 
import redis 

from twitter__util import getRedisIdByScreenName 

# A pretty-print function for numbers 
from twitter__util import pp 

r = redis.Redis() 
screen_names=['user1','user2'] 
def friendsFollowersInCommon(screen_names): 
    r.sinterstore('temp$friends_in_common', 
     [getRedisIdByScreenName(screen_name, 'friend_ids') 
     for screen_name in screen_names] 
    ) 

r.sinterstore('temp$followers_in_common', 
    [getRedisIdByScreenName(screen_name, 'follower_ids') 
    for screen_name in screen_names] 
) 

print 'Friends in common for %s: %s' % (', '.join(screen_names), 
             pp(r.scard('temp$friends_in_common'))) 

print 'Followers in common for %s: %s' % (', '.join(screen_names), 
              pp(r.scard('temp$followers_in_common'))) 

# Clean up scratch workspace 

r.delete('temp$friends_in_common') 
r.delete('temp$followers_in_common') 

if __name__ == "__main__": 
    if len(screen_names) < 2: 
     print >> sys.stderr, "Please supply at least two screen names." 
     sys.exit(1) 

friendsFollowersInCommon(screen_names[1:]) 

回答

1

$符號僅僅是一個鍵名稱的一部分。它分隔名稱部分。我通常使用:爲了同樣的目的(例如users:123

%s部分是string formatting

+0

這部分怎麼樣? r.sinterstore('temp $ followers_in_common', [getRedisIdByScreenName(screen_name,'follower_ids') for screen_name in screen_names] – user1511208 2012-07-23 10:03:39

+0

那麼,它呢? – 2012-07-23 10:05:48

+0

我的意思是,[]第一部分是方法然後for循環。這是什麼意思? – user1511208 2012-07-23 10:11:23