dict = {10:50,2:12,4:43} item = 12 for dict.keys()中的键: 如果dict [key] == item: 打印键 break ======== ========== 解决方案 Egor>我知道如何按键获得物品 ... Egor>但我想知道如何逐项获取 假设你的字典定义了一对一的映射,只需将其反转: forward = {10:50,2:12,4:43} reverse = dict([(v,k)for(k,v)在forward.iteritems()])打印转发 {10:50,4:43,2:12}打印反向 {50:10,43:4,12:2} 这会使你的存储空间增加一倍,所以你必须将它与 $进行交易。 b $ b无需循环遍历整个字典的速度增加。 Skip 在文章< ma ** ************************************@python.o rg>, Skip Montanaro< sk ** @ pobox.com>写道: Egor>我知道如何按键获得物品 ... Egor>但我想知道如何逐项获取 假设你的字典定义了一对一的映射,只需将其反转: >>> forward = {10:50,2:12,4:43} >>> reverse = dict([(v,k)for(k,v)in forward.iteritems()])>>>向前打印{10:50,4:43,2:12}>>>打印反向 {50:10,43:4,12:2} 这会使你的存储空间加倍,所以你必须交换它反对不必在整个字典上循环的速度增加。 好吧,你*做*遍历整个字典,但你只做它一次, 当你创建反向字典时。如果你只打算进行单一的b 查询,那就没有收获,但如果你通过多次查询分摊成本, 几乎可以肯定一个大赢家。 这提出了一个有趣的问题。让我们假设您在进行任何查找之前将所有 条目添加到字典中,然后您需要 来向两个方向查找内容。哪个更快,同时建立前向和反向双击,或者只是建立前进的一个,当你完成这个时,建立反向 一次性完成以上列表理解? 顺便说一下,Python真的构建了中间列表并将其扔掉 $ b使用它来初始化字典后的$ b,还是足够智能 知道它不需要在内存中构建整个列表? 这会使你的存储空间增加一倍,所以你必须将其交换掉,以获得无需循环的速度增益整个字典。 Roy>好吧,你*做*循环整个字典,但你只做它 罗伊>一次,当你创建反向字典时。如果你只想去b $ b罗伊>做一次查询,这没有收获,但如果你摊还成本超过 Roy>很多查询,它几乎肯定是一个巨大的胜利。 当然,但OP说他的字典很大。由他决定是否值得(或者甚至可能)时空权衡取决于他。 罗伊>顺便说一下,Python真的构建了中间列表并将其抛出 Roy>在用它来初始化字典之后离开,还是很聪明 Roy>足以知道它并不需要构建整个列表 Roy>在记忆中? 这就是我在我的例子中调用.iteritems()的原因。它不会产生 整个元组列表,因为.items()会。 跳过 saluton al ciuji know how to get item by key==================dict = {10 : 50, 2 : 12, 4 : 43}print dict[2] 12but i wonder how to get key by itemprint dict[12] 2==================is there a more fast way than that one (my dictionary is really big)==================dict = {10 : 50, 2 : 12, 4 : 43}item = 12for key in dict.keys():if dict[key] == item:print keybreak================== 解决方案Egor> i know how to get item by key...Egor> but i wonder how to get key by itemAssuming your dictionary defines a one-to-one mapping, just invert it: forward = {10 : 50, 2 : 12, 4 : 43} reverse = dict([(v,k) for (k,v) in forward.iteritems()]) print forward{10: 50, 4: 43, 2: 12} print reverse{50: 10, 43: 4, 12: 2}That doubles your storage, so you''ll have to trade that off against thespeed gain of not having to loop over the entire dictionary.SkipIn article <ma**************************************@python.o rg>,Skip Montanaro <sk**@pobox.com> wrote: Egor> i know how to get item by key ... Egor> but i wonder how to get key by item Assuming your dictionary defines a one-to-one mapping, just invert it: >>> forward = {10 : 50, 2 : 12, 4 : 43} >>> reverse = dict([(v,k) for (k,v) in forward.iteritems()]) >>> print forward {10: 50, 4: 43, 2: 12} >>> print reverse {50: 10, 43: 4, 12: 2} That doubles your storage, so you''ll have to trade that off against the speed gain of not having to loop over the entire dictionary.Well, you *do* loop over the entire dictionary, but you only do it once,when you create the reverse dict. If you are only going to do a singlelookup, it''s no gain, but if you amortize the cost over many lookups,it''s almost certainly a big win.This raises an interesting question. Let''s assume that you add all theentries to the dictionary before you do any lookups, and you then needto lookup things up in both directions. Which is faster, tosimultaneously build both the forward and reverse dicts, or to justbuild the forward one and when you''re done doing that, build the reverseone in a single shot with the above list comprehension?BTW, does Python really build the intermediate list and throw it awayafter using it to initialize the dictionary, or is it smart enough toknow that it doesn''t really need to build the whole list in memory? That doubles your storage, so you''ll have to trade that off against the speed gain of not having to loop over the entire dictionary.Roy> Well, you *do* loop over the entire dictionary, but you only do itRoy> once, when you create the reverse dict. If you are only going toRoy> do a single lookup, it''s no gain, but if you amortize the cost overRoy> many lookups, it''s almost certainly a big win.Sure, but the OP said his dictionary was big. It''s up to him to decidewhether the space-time tradeoff is worth it (or even possible).Roy> BTW, does Python really build the intermediate list and throw itRoy> away after using it to initialize the dictionary, or is it smartRoy> enough to know that it doesn''t really need to build the whole listRoy> in memory?That''s why I called .iteritems() in my example. It won''t generate theentire list of tuples as .items() would.Skip 这篇关于[字典]如何逐项获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-31 02:10