Check if two nested lists are equal when replacing

In some context, I'm trying to calculate the number of unique situations that can arise when calculating Banzhaf power indicators for four players when there is no dictator, and there are four or five winning coalitions.

I use the following code to create the set of lists I want to iterate over.

from itertools import chain, combinations

def powerset(iterable):
    s = list(iterable)
    return chain.from_iterable(map(list, combinations(s, r)) for r in range(2, len(s)+1))

def superpowerset(iterable):
    s = powerset(iterable)
    return chain.from_iterable(map(list, combinations(s, r)) for r in range(4, 6))

set_of_lists = superpowerset([1,2,3,4])

However, the two lists in this set should not be considered unique if they are equivalent when reassigned.

Using the following list as an example:

[[1, 2], [1, 3], [2, 3], [1, 2, 4]]

If each item is 2renamed to 3and vice versa, we get:

[[1, 3], [1, 2], [3, 2], [1, 3, 4]]

The order in each sub-list is not important, and the order of the subscriptions is also not important. Thus, the swapped list can be rewritten as:

[[1, 2], [1, 3], [2, 3], [1, 3, 4]]

4 , P (4,4) = 24 , ( ).

? , , ?

, ( ). , ( ), frozenset .

: , tobias_k, "", , , , .

+4
1

, , , .

"", "" . , , , , . , , , , , - . , .

def normalize(lists):
    items = set(x for y in lists for x in y)
    counter = itertools.count()
    sorter = lambda x: sorted(len(y) for y in lists if x in y)
    mapping = {k: next(counter) for k in sorted(items, key=sorter)}
    return tuple(sorted(tuple(sorted(mapping[x] for x in y)) for y in lists))

"" :

>>> normalize([[1, 2], [1, 3], [2, 3], [1, 2, 4]])
((0, 1), (0, 2), (1, 2), (1, 2, 3))
>>> normalize([[1, 3], [1, 2], [3, 2], [1, 3, 4]])
((0, 1), (0, 2), (1, 2), (1, 2, 3))

330 36. , , .

>>> normalized = set(map(normalize, set_of_lists))
>>> len(normalized)
36
+1

All Articles