在python中,如何在单词的key:value对的值中存储大的整数值?

2024-04-26 13:55:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含字符键和整数值的python字典。我想存储任意大的整数作为键的值。我可以在字典中存储的作为键值的最大整数是多少?在初始化字典时,是否可以定义字典的数据类型? 例如,在C中,我们做map<string, long long int>。我应该如何用python声明同一个字典

例如,在下面的代码中,对于许多键,“我的值”将大大增加

 for w in words:
     ngrams_w = self.word_to_ngrams(w)
        for n in ngrams_w:
            if n in lookup_table:
                lookup_table[n] = lookup_table[n] + 1
            else:
                lookup_table[n] = 1
     return lookup_table

现在我的语料库中有这么多的单词,导致了太多的三重格。那么,我的查找表中的值能够容纳无限大的整数吗


Tags: inmapforstring字典定义table整数
1条回答
网友
1楼 · 发布于 2024-04-26 13:55:03

Python整数可以任意大——它们可以占用的字节量仅受可用内存的限制。因此,您可以在DICT中存储非常大的整数,就像任何其他类型的变量一样

还要注意python的类型是幕后的。同样的dict可以使用几乎任何东西作为键,也可以使用任何东西作为相应的值。你不需要提前申报

例如:

large_dict = {
   'a': 2**99**2,
   'b': 3**99**2,
   'c': 4**66**3,
   'd': 5,
}

print(large_dict['a'])
# 24830616513292456149616454036974739771820938966442197939419359658089567202400780743905571137028486156486036903513607264042124719153572110201314197883546916952215606391372422139042592773840794323335212159700095246665013394384789465765464293679828325113232950453141468484569985222217035575296458501452872186378717438026640856834533121910412608973480242085881672164719912544082874072107422434390554486837170594217552352179217838815153995983301946373496587090616156896354994345352377952726227907079690576457293694283595586693944067261016834086680506973471228878547284373711902581003346534526356682248040411471279547066667078693613059243075566078841931213128462480543351921983621089668275721435948101887626265184182951445127840725828830209648532962314294583129458462587616879683621002666459872557839185108646816817833525155517060333482860732993614665277625489415502061774379920496929429602780585502167079135606858316403456987934488806952870800041077801533837170751376374861251329800483795256882616805854878581164361680226869833419669385928257122731195573904839938024027782370149377710658645650280419249518679276644090004956974913792951678647959778713113374754349884308332520403967474904860378939613020574276565212696855826404212709820546399167642334178336428555942563152453083892489382421641854517289230831100263608004179956903465008511600241582320500793382331980382057875456793451053160479393167189744446997815108094874623772314046414880967888630771591139242793679368373241262303020403503806741495927267535782756256226464827679171453103936982984219779689528492725634758390287762884938541414982295293195059435820985171981953652126800809143528930914867233895403413631144957773050572055247562723249843368842707168818290935719822151262157837630513192298577647355462098479363787742148415773842359155842482339588691450667863720967596151398289089712023294064255959682719606426598201025203109875583565132643670912933398263577545142697715765285834571917043394759092957857788427530930354248022105571235052694964834870559303479169142265964947215835534331232659231738844605321566863791868404356910674470711233474169254178106049934124965008926126859691353260188518891725702238011978956872340932416194028852429366663313267993063866935339039988538965602564065890425970977641916796977494816019907846068887159081996367805130377827078940214763737954464021880895504378918548182271578043448659561293769460226209756884091241789271212624443566823618854822556818054950921281495213677124939344997301596883289597036555187701021490864067003001042353911716339896584757966357696031609808169050800581900702426733604379398309018563404211559969917703040652231206637777868534421416141767113165973592413599591681134708842927135950028572565259146431359982767414698579766187600152269764793150959658673104646366308004593390529802053032008317417654941763892718192450423219923744313363894743858416682079171193877076995395458797611449453319718948222265568320631084026777091936418337638425034752

考虑到试图打印它们会溢出我的终端窗口的大小,并且large_dict['c']显示良好,尽管消耗了StackOverflow大约六倍的字符限制,这些应该被限定为“任意大的整数”。请注意,当您尝试将它们转换为float点编号时,可能会出现问题,因为这些点编号有精度限制


Python确实支持“类型暗示”,即可以暗示变量的类型,但实际上并不影响对代码所能做的任何事情。不管怎样,以下是您将如何表示这一点:

# need to import Dict to type-hint about it
from typing import Dict

# hint that large_dict should use strings as keys and ints as values
# note that a 'character' is just a 1-length string, as far as python is concerned
# note also that this will not stop someone from putting a non-string in as a key, 
#   or a non-int in as a value
large_dict: Dict[str, int] = {
    ...
}

相关问题 更多 >