pfe*_*ate 17 python dictionary
我想知道Python是否对字典键的长度有限制.
为了澄清,我不是在谈论密钥的数量,而是每个密钥的长度.我将基于动态值(验证后)构建我的词典,但我不确定在这种情况下是否应该考虑长度.
Hug*_*ell 19
这是一些示例代码:
from string import ascii_letters
from random import choice
def make_str(length):
return "".join(choice(ascii_letters) for i in range(length))
test_dict = {make_str(10000000): i for i in range(5)}
Run Code Online (Sandbox Code Playgroud)
结论:Python非常乐意使用一个1000万字符的字符串作为dict键.