Questions tagged [frozenset]

questions related to frozenset objects in Python

frozenset is a built-in type for Python programming language. The elements of a set must be hashable. To represent sets of sets, the inner sets must be frozenset objects.

A set is a collection in which no element is repeated. It is often implemented by hashing the objects as they are added to the set, and comparing against those hashes for operations on the set.

See also

Read more here

85 questions
3
votes
1 answer

Is it possible to create frozen sets with curly braces?

It appears that one can only create a frozenset using the frozenset() constructor. Is this true or is there a textual way to create a frozen set? I can find no evidence one way or the other in the docs.
Ray Salemi
  • 5,247
  • 4
  • 30
  • 63
3
votes
1 answer

numpy.unique has the problem with frozensets

Just run the code: a = [frozenset({1,2}),frozenset({3,4}),frozenset({1,2})] print(set(a)) # out: {frozenset({3, 4}), frozenset({1, 2})} print(np.unique(a)) # out: [frozenset({1, 2}), frozenset({3, 4}), frozenset({1, 2})] The first out is correct,…
klapeyron
  • 502
  • 7
  • 18
3
votes
2 answers

Extract Frozenset items from Pandas Dataframe

I have the following dataframe: And I would like to convert the columns "antecedents" and "consequents" to string, removing the "frozenset({ ... })" format and thus have, for all the rows: "VENTOLIN S.INAL200D 100MCG", instead of frozenset({…
Alessandro Ceccarelli
  • 1,775
  • 5
  • 21
  • 41
3
votes
2 answers

Time-complexity of checking if two frozensets are equal in Python

Couldn't find the details of this anywhere online, when comparing two frozensets does Python iterate through the elements in one of the sets or does it check the hash values of the frozensets since frozensets are hashable?
SW Williams
  • 559
  • 1
  • 5
  • 18
3
votes
1 answer

Add the empty set to a family of sets in a frozenset in python

Suppose I generate a frozenset with A = frozenset(frozenset([element]) for element in [1,2,3]) And I have the empty set E = frozenset(frozenset()) Now I want to have the union of both sets: U = A | E This gives me frozenset({frozenset({2}),…
Johannes Bleher
  • 321
  • 3
  • 15
3
votes
1 answer

Apply a function pairwise on a pandas series

I've a pandas series whose elements constitute frozensets: data = {0: frozenset({'apple', 'banana'}), 1: frozenset({'apple', 'orange'}), 2: frozenset({'banana'}), 3: frozenset({'kumquat', 'orange'}), 4: frozenset({'orange'}), …
cs95
  • 379,657
  • 97
  • 704
  • 746
3
votes
2 answers

Accessing items from a frozenset in python

I have a frozenset given as x = frozenset({"a":1,"b":2}). I am not able to figure out a way to be able to access the items in the dict. Is there a way to unfreeze the frozenset? Given below is the error I get. In [1]: x =…
lordlabakdas
  • 1,163
  • 5
  • 18
  • 33
3
votes
4 answers

frozenset at least x elements

I currently have this code, it checks if all elements in the array are the same. If this is the case, return true def all_equal(lst): """ >>> all_equal([1,1,1,1,1,1,1]) True >>> all_equal([1,2,3,1]) False """ return len(frozenset(lst))…
Sharpless512
  • 3,062
  • 5
  • 35
  • 60
2
votes
1 answer

Extending FrozenSet from typing

I'm probably doing something dumb here. For those of you who want to copy and paste, make sure that you: from typing import * I'm using Python 3.7.4. This: class S(FrozenSet[str]): def __init__(self, strs: Iterable[str], name: str): …
2
votes
1 answer

In python, frozenset's subclass's __init__ method throw TypeError of arguments number

The class's __init__ method has 3 arguments, but when I instantiate it with 3 arguments, it throw an error that it expected 1 arguments. I cannot undrestand. class ArrObj(frozenset): def __init__(self, elem_list, elem_count, self_count): …
xupeng
  • 67
  • 4
2
votes
2 answers

"Multiton" implementation of frozensets - just one instance per value

How can I implement the Multiton design pattern for frozensets, in a way which works no matter how the frozenset is created? What I'm looking for is class that behaves just like frozenset, but that gaurantees "full interning": for any two instances,…
Just Me
  • 413
  • 3
  • 9
2
votes
1 answer

Why this class constructor raises this error?

Having this class: class A(frozenset): def __init__(self, *args): frozenset.__init__(self, *args) Executing A(range(2)) results in the following error: Traceback (most recent call last): File "", line 1, in
matteo_c
  • 1,990
  • 2
  • 10
  • 20
2
votes
1 answer

Finding substring in pandas frozenset

I'm trying to find a substring in a frozenset, however I'm a bit out of options. My data structure is a pandas.dataframe (it's from the association_rules from the mlxtend package if you are familiar with that one) and I want to print all the rows…
ch1ll
  • 419
  • 7
  • 20
2
votes
2 answers

Sorting a list of python sets by value

The frozenset docs says: The frozenset type is immutable and hashable — its contents cannot be altered after it is created; it can therefore be used as a dictionary key or as an element of another set. However, the docs for for python sets…
Travis Black
  • 705
  • 7
  • 18
2
votes
1 answer

dataframe to frozenset

I want to translate a dataframe to frozensets and keep the dataframe columns within the frozenset. Example x=pd.DataFrame(data=dict(sample=["A","B","C"],lane=[1,1,2])) >>> x lane sample 0 1 A 1 1 B 2 2 C And I would…
Nicolas Rosewick
  • 1,938
  • 4
  • 24
  • 42