Use frozenset as a pair in python

2019-03-16 06:02发布

I would like to make a pair of two elements. I don't care about the order of the elements, so I use frozenset.

I can think of the following two methods to iterate the elements back from the frozenset. Isn't there any fancier method? Thanks in advance.

pair = frozenset([element1, element2])
pair2 = list(pair)
elem1 = pair2[0]
elem2 = pair2[1]
pair = frozenset([element1, element2])
elems = []
for elem in pair:
    elems.append(elem)
elem1 = elems[0]
elem2 = elems[1]

4条回答
Luminary・发光体
2楼-- · 2019-03-16 06:33

Just to elaborate on an above comment, and assuming your elements are easily sortable, you could make an unordered pair class from tuple using:

class Pair(tuple):
    def __new__(cls, seq=()):
        assert len(seq) == 2
        return tuple.__new__(tuple, sorted(seq))

Then you get:

>>> Pair((0, 1))
(0, 1)

>>> Pair((1, 0))
(0, 1)

>>> Pair((0, 1)) == Pair((1, 0))
True
查看更多
贪生不怕死
3楼-- · 2019-03-16 06:45

If it is just two elements you are de-sequence them. But I am not sure, what you are trying to do here with the frozenset

>>> s = frozenset([1,2])
>>> s
frozenset({1, 2})
>>> x,y = s
>>> x
1
>>> y
2
查看更多
对你真心纯属浪费
4楼-- · 2019-03-16 06:47
pair = frozenset([element1, element2])
elem1, elem2 = pair
查看更多
一夜七次
5楼-- · 2019-03-16 06:49

If you have a lot of those pair things, using frozenset() is NOT a good idea. Use tuples instead.

>>> import sys
>>> fs1 = frozenset([42, 666])
>>> fs2 = frozenset([666, 42])
>>> fs1 == fs2
True
>>> t1 = tuple(sorted([42, 666]))
>>> t2 = tuple(sorted([666, 42]))
>>> t1 == t2
True
>>> sys.getsizeof(fs1)
116
>>> sys.getsizeof(t1)
36
>>>

Update Bonus: sorted tuples have a predictable iteration sequence:

>>> for thing in fs1, fs2, t1, t2: print [x for x in thing]
...
[42, 666]
[666, 42]
[42, 666]
[42, 666]
>>>

Update 2 ... and their repr() is the same:

>>> repr(fs1)
'frozenset([42, 666])'
>>> repr(fs2)
'frozenset([666, 42])' # possible source of confusion
>>> repr(t1)
'(42, 666)'
>>> repr(t2)
'(42, 666)'
>>>
查看更多
登录 后发表回答