-1

I am working with expression trees which are equivalent to each other considering commutativity. To bring about this equivalence, I thought I will use frozen multisets for holding the operands as they are hashable (unlike dicts), unordered (unlike tuples), and allow for repeated elements (unlike sets).

But the frozen multisets don't seem to work as I imagined. They are treating repeated elements as a single element.

FrozenMultiset treating repeated elements as the same

Am I missing something here? Please help.

Or please suggest alternatives which serve my purpose. Thanks in advance.

@dudulu I tried print(fms({3,4,3,1}) is fms({1,4,3,3})) and it still returned False.

enter image description here

Solved.

I used a tuple to represent the operands instead of a set. It worked.

enter image description here

Josh
  • 21
  • 5

1 Answers1

0

As @Davis Herring pointed out, {3,3} notation is for set literals. So the ForcedMultiset treats its contents as a set would. Using tuples (3,3) or lists [3,3] instead solves the problem.

print(fms((3,4,3,1)) == fms((1,4,3,3))) returns True.

Josh
  • 21
  • 5
  • 1
    More precisely, `fms` receives a 1-element set as its argument; it has no idea what *syntax* was used to create that set. – chepner Aug 28 '22 at 15:06