【问题标题】:What is the alphanumeric id in a reddit URL?reddit URL 中的字母数字 ID 是什么?
【发布时间】:2010-09-29 10:58:01
【问题描述】:

reddit 网址中的7n5lu 是什么

http://www.reddit.com/r/reddit.com/comments/7n5lu/man_can_fly_if_you_watch_one_video_in_2

...它是如何产生的?

更新: @Gerald,我最初认为这是对 id 的一些混淆。它只是将整数转换为更紧凑的表示。我在想,为什么要这样做?为什么不使用原始整数本身!

>>> to36(4000)
'334'
>>> to36(4001)
'335'

【问题讨论】:

  • 如果你使用带字母的数字,最后的字符串会更短。例如:to36(9) == '9' to36(10) == 'a'

标签: python url slug reddit


【解决方案1】:

reddit 源码is available!这是我发现生成该字符串的内容:

def to_base(q, alphabet):
    if q < 0: raise ValueError, "must supply a positive integer"
    l = len(alphabet)
    converted = []
    while q != 0:
        q, r = divmod(q, l)
        converted.insert(0, alphabet[r])
    return "".join(converted) or '0'

def to36(q):
    return to_base(q, '0123456789abcdefghijklmnopqrstuvwxyz')

以及其他地方,在“链接”类下:

@property
def _id36(self):
    return to36(self._id)

【讨论】:

  • 注意他们有一个小错误:零情况假设零是“0”。最后一行应以“或字母[0]”结尾。
【解决方案2】:

这看起来像是线程的唯一 ID。它最有可能用于在数据库中查找线程。

【讨论】:

    【解决方案3】:

    它是一个整数,以 36 为基数。id 是按顺序生成的。例如,id 89 后面的评论是 id 8a 等。鉴于此,您不需要 URL 中的任何其他信息。

    理论上,评论、帖子、消息、用户和 subreddit 可以具有相同的 ID 并计数。您可以通过它们的“全名”(即事物的类型、下划线和下划线)来区分它们。

    t1 是评论,t2 是用户,t3 是提交,t4 是消息,t5 是 subreddit,t6 是奖励,ModAction 是版主操作,ModmailConversation 是一个 modmail 对话等。

    更多信息请参见the API documentation

    【讨论】:

      【解决方案4】:

      一点评论。

      对于这个例子是不够的,但通常附加到列表中

      a = []
      for i in range(NNN): a.append(i)
      a.reverse()
      

      确实比在头部插入更有效。

      a = []
      for i in range(NNN): a.insert(0,i)
      

      .

      【讨论】:

        猜你喜欢
        • 2012-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-11
        相关资源
        最近更新 更多