简体   繁体   中英

Why are frozen multisets in python not considering repeated elements?

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 将重复元素视为相同

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.

在此处输入图像描述

Solved.

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

在此处输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM