当键是位掩码时如何反转dict?

时间:2018-02-07 14:52:27

标签: python python-3.x

如何在编译期间反转Flag_to_Marker

class FLAG(IntEnum):
    DEFAULT_ZERO = 0b0
    L1 = 0b01
    L2 = 0b10
    RSU = 0b100
    ESCALATED_COMMENTS = 0b1000

    Flag_to_Marker = {L1: 'is_l1', L2: 'is_l2', RSU: 'is_rsu'}
    Marker_to_Flag = {v: k for k, v in Flag_to_Marker.items()}

    def marker(self, flag):
       return self.Flag_to_Marker[flag]

    def flag(self, marker):
       return self.Marker_to_Flag[marker]

但是当我尝试构建Marker_to_Flagint() argument must be a string, a bytes-like object or a number, not 'dict'时,我遇到以下异常。如何解决?

我希望Marker_to_Flag = {'is_l1': L1, 'is_l2': L2, 'is_rsu': RSU}

2 个答案:

答案 0 :(得分:-2)

Marker_to_Flag = {v: k for k, v in Flag_to_Marker.items()}

答案 1 :(得分:-2)

  1. 您的价值观必须是唯一的。
  2. reversed_dict = {y:x代表x,y代表Flag_to_Marker.items()}