reddit URL 中的 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'
最佳答案
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')
和其他地方,在“Link”类下:
@property
def _id36(self):
return to36(self._id)
关于python - reddit URL 中的字母数字 ID 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/410485/