【问题标题】:python and redis, unclear syntaxpython和redis,语法不清楚
【发布时间】:2012-07-23 09:32:19
【问题描述】:

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

我是这个领域的新手,也是 Redis 的新手。我想了解$ 在这种情况下的含义。还有%s的print,是什么意思?

这是下面的源代码(来自: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:])

【问题讨论】:

    标签: python redis mining


    【解决方案1】:

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

    %s 部分是string formatting

    【讨论】:

    • 这部分怎么样? r.sinterstore('temp$followers_in_common', [getRedisIdByScreenName(screen_name, 'follower_ids') for screen_name in screen_names]
    • 我的意思是,[]第一部分是方法,然后是for循环。这是什么意思?
    • 这是python的列表理解。我建议你在学习新技术(redis)之前先熟悉语言(python)。
    猜你喜欢
    • 2017-05-28
    • 2011-08-14
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    相关资源
    最近更新 更多