I am working in Python. I have a large list L = [X_{1}, X_{2}, ....,X_{n}]
where each X_{i}
is a set. It is possible that for some distinct indices i, j
we have X_{i} = X_{j}
. I want to create a list (or a set) L_{1} = [Y_{1}, Y_{2}, ... ,Y_{k}]
such that the set (in the mathematical sense) {X_{1},..., X_{n}} = {Y_{1}, ..., Y_{k}}
and L_{1}
is of least possible length (in other-words L_{1}
has no repetitions).
For subsequent applications I would need to access the elements of L_{1}
. So I cannot use the frozenset
option.
Using set(L)
gives an error TypeError: unhashable type: 'set'
.
I think I can solve it using a loop construction but I am wondering if there is a more elegant solution to this problem in Python.
If you want to eliminate duplicate sets, you'll need to convert them to frozenset()
instances:
unique_sets = {frozenset(s) for s in L}
or if you are using Python 3:
unique_sets = set(map(frozenset, L))
If you need to end up with a list of mutable sets again you can produce a list of set()
objects again with:
unique_set_list = [set(s) for s in unique_sets]
You could combine the two into a single list comprehension with a little loss of readability:
seen = set()
unique_set_list = [ms for ms, ims in ((s, frozenset(s)) for s in L)
if not (ims in seen or seen.add(ims))]
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.