首页 > 解决方案 > Adding frozenset to set of other frozensets

问题描述

I am trying to add a frozenset to an already existing set of frozensets however when i try to use the add() function to add it the return is None. I tried using the update() function instead but to no avail. I am forced to use frozensets because I need a set of sets and this seems like the only solution in Python. The literal is just a list of one element of type String.

    print(literal)
    print(clauses)
    clauses = clauses.add(frozenset(literal))
    print(clauses)

The output looks like this:

['!y']
{frozenset({'!y', 'z', 'x'})}
None

标签: pythonpython-3.xsetfrozenset

解决方案


The general rule (https://docs.python.org/3/library/stdtypes.html)

The methods that add, subtract, or rearrange their members in place, and don’t return a specific item, never return the collection instance itself but None.

That's why:

clauses = clauses.add(frozenset(literal))

means:

clauses.add(frozenset(literal))
clauses = None

推荐阅读