【问题标题】:Searching for a tuple within a dict在字典中搜索元组
【发布时间】:2016-04-12 16:42:44
【问题描述】:

我的 Python 代码中有一个元组结构,它声明了以下内容:

match_entry = (util.frozendict(rule_match), priority, version)

当我打印 match_entry 时,我看到以下内容:

print match_entry
({'switch': 1, 'dstmac': 00:00:00:00:00:01, 'srcmac': 00:00:00:00:00:01}, 60000, 5)

我正在寻找这个特殊的元组,一个元组的字典,我们称它为 dict_of_tuples; dict的相应输出如下。

print dict_of_tuples

{({'switch': 5, 'dstmac': '00:00:00:00:00:00', 'srcmac': '00:00:00:00:00:01'}, 59999, 7): [CountBucket 140271056467472, CountBucket 140271056411280], ({'switch': 5, 'dstmac': '00:00:00:00:00:00', 'srcmac': '00:00:00:00:00:01'}, 59999, 5): [CountBucket 140271056467472, CountBucket 140271056411280], ({'switch': 1, 'dstmac': '00:00:00:00:00:01', 'srcmac': '00:00:00:00:00:01'}, 60000, 5): [CountBucket 140271057099664, CountBucket 140271056501008]}

但是,当我检查匹配项是否在元组中时:

if match_entry in dict_of_tuples:

我没有看到任何结果,即使 match_entry 显然在 dict_of_tuple 中。有什么理由会出现这种情况?

【问题讨论】:

  • 确定它们是字典和元组而不是字符串?
  • 'srcmac' 关联的值在一种情况下似乎是字符串,而在另一种情况下似乎是某种非字符串对象。
  • bdw,00:00:00:00:00:01 是什么?我知道"00:00:00:00:00:01" 是什么,但不是前者。
  • 完整的代码会更好。否则很难复制该场景

标签: python dictionary tuples


【解决方案1】:
        import frozendict as util
        from collections import defaultdict

        ### Create the first entry using hashable frozendict
        match = util.frozendict({'switch': 1, 'dstmac': '00:00:00:00:00:01', 'srcmac': '00:00:00:00:00:01'})
        match_array = tuple[match,60000,5]
        count_bucket2 = dict(CountBucket1 = 140271057099664, CountBucket2 = 140271056501008)

        ### Create the second entry using hashable frozendict
        match_entry = util.frozendict(switch= 5, dstmac= '00:00:00:00:00:00', srcmac= '00:00:00:00:00:01')
        match_array1 = tuple([match_entry, 59999, 7])

        count_bucket1 = dict(CountBucket1 = 140271056467472, CountBucket2 = 140271056411280)

        # Initialize the dictionary of tuples
        dict_of_tuples = ({tuple(match_array) : count_bucket2},{tuple(match_array1) : count_bucket1})

        ####### Your match entry
        match_entry = [{'switch': 1, 'dstmac': '00:00:00:00:00:01', 'srcmac': '00:00:00:00:00:01'},60000,5]                

    #Treating the final structure as a tuple. Each element of the tuple is a #dictionary.
        k = 0
        while k < len(dict_of_tuples):
            key = dict_of_tuples[k].iterkeys()
            val = dict_of_tuples[k].itervalues()
            if key.next() == tuple(match_entry):
                print ('Has key','corresponding value',val.next())
            else:
                print "Key not present"
            k+= 1

【讨论】:

  • 将最终结构视为元组。元组的每个元素都是一个字典。
【解决方案2】:

字典不可散列,所以我认为你的结构可能是不可能的:

>>> {({1:2, 2:3}, 2, 3): [1,2,3]}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'dict'

字典不允许字典存在于其键中。所以确保它真的是一个字典而不是字符串。

编辑

正如mentioned 在评论中一样,冻结的字典(不可变字典)将是可散列的,因此解决了这个问题。但是你必须修改默认的字典类型。通常,您可以在运行时修改字典:

d = {}
d["key"] = "val"

Object 中使用__setattr__ 启用。当我们需要不可变的类时?基本上,不可变对象在内存上的效率更高。这是a topic 关于这个的。

【讨论】:

  • 里面的dict是一个冻结的dict。所以它是可散列的。
  • @CPanda 啊,新知识。但我检查了这个:stackoverflow.com/questions/2703599/what-would-a-frozen-dict-be,测试确实失败了。我记得可以通过手动修改__slot__来创建冻结对象。
  • 对不起,伙计,我的错。我一直在考虑冷冻套装。
  • 谢谢,由于 FrozenDicts,我没有遇到任何类型错误。原来这是因为 srcmac 在前面的步骤中被转换为字符串。
猜你喜欢
  • 2017-09-15
  • 2014-08-01
  • 2018-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 2017-11-30
  • 1970-01-01
相关资源
最近更新 更多