python和redis,语法不清楚

时间:2012-07-23 09:32:19

标签: python redis mining

这是挖掘社交网络一书中代码的副本。

我是这个领域的新手,也是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 个答案:

答案 0 :(得分:1)

$符号只是键名的一部分。它分隔名称部分。我通常使用:用于相同目的(例如users:123

%s部分是string formatting