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
59
votes
2 answers

Set vs. frozenset performance

I was tinkering around with Python's set and frozenset collection types. Initially, I assumed that frozenset would provide a better lookup performance than set, as its immutable and thus could exploit the structure of the stored items. However, this…
Sven Hager
  • 3,144
  • 4
  • 24
  • 32
26
votes
3 answers

Is it safe to use frozen set as Dict key?

It obviously works but are there cases where two sets of same elements happen to add two entries in Dict? I guess I got this condition earlier and changed my code from frozenset(...) to tuple(sorted(frozenset(...))). Can someone who knows how Dict…
balki
  • 26,394
  • 30
  • 105
  • 151
21
votes
5 answers

What are the differences between Set, FrozenSet, MutableSet and AbstractSet in python typing module?

I am trying to annotate my code with types but I am a little confused when it comes to sets. I read some points in PEP 484: Note: Dict , List , Set and FrozenSet are mainly useful for annotating return values. For arguments, prefer the abstract…
marcotama
  • 1,991
  • 2
  • 19
  • 24
19
votes
5 answers

'frozenset' object is not callable

When I attempt to import hashlib in any context, it throws this error: File "", line 1, in File "build/bdist.macosx-10.11-intel/egg/hashlib.py", line 115, in """ TypeError: 'frozenset' object is not callable Any idea…
Alex Beals
  • 1,965
  • 4
  • 18
  • 26
11
votes
3 answers

When would a frozenset be useful?

What are some examples of when using a frozenset would be the best option?
Phoenix
  • 4,386
  • 10
  • 40
  • 55
10
votes
4 answers

Maintaining the order of the elements in a frozen set

I have a list of tuples, each tuple of which contains one string and two integers. The list looks like this: x = [('a',1,2), ('b',3,4), ('x',5,6), ('a',2,1)] The list contains thousands of such tuples. Now if I want to get unique combinations, I…
enterML
  • 2,110
  • 4
  • 26
  • 38
6
votes
2 answers

Extract string from rules frozensets

With the following statement: rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1.2) I get a data frame of rules in the format: frozenset({'Co_Apples'}) But I need to extract a Co_Apples as a string. How can I do that?
5
votes
4 answers

Which takes less memory, a frozenset or a tuple?

I have an object which needs to be "tagged" with 0-3 strings (out of a set of 20-some possibilities); these values are all unique and order doesn't matter. The only operation that needs to be done on the tags is checking if a particular one is…
Draconis
  • 3,209
  • 1
  • 19
  • 31
5
votes
1 answer

Complexity of converting a set to a frozenset in Python

What is the computational complexity of "freezing" a set in Python? For example, does the second line in a = {1,2,3} b = frozenset(a) require O(n) time? Or is it simply a "view" created in constant time?
erensezener
  • 847
  • 1
  • 7
  • 10
5
votes
2 answers

Python - issue with using a list of frozenset entries in a for loop

I am trying to learn the apriori machine learning algorithm from a book that uses Python, and as part of that learning, I am currently stuck with this following problem: The following code construct seems to work fine: Ck = [[1], [2], [3], [4],…
5
votes
1 answer

Different python frozensets with same hash value

My understanding is that hashing two different frozensets (immutable Python sets), which need to contain hashable objects, should lead to two different hashes. Why do I get the output below for two different frozensets? In [11]: a Out[11]:…
Roy
  • 3,574
  • 2
  • 29
  • 39
4
votes
2 answers

How can you use property setter when using frozen dataclasses in Python

I was just playing around with the concept of Python dataclasses and abstract classes and what i am trying to achieve is basically create a frozen dataclass but at the same time have one attribute as a property. Below is my code for doing so: import…
4
votes
2 answers

How is frozenset equality implemented in Python?

How is frozenset equality implemented in CPython? In particular, I want to know how individual elements in the fronzenset are compared with each other and their total time complexity. I took a look at set and frozenset difference in implementation…
James Parker
  • 457
  • 4
  • 19
4
votes
2 answers

filter on a pandas dataframe column which contains frozenset of strings

I've a result dataframe which I've obtained like this (ref http://rasbt.github.io/mlxtend/user_guide/frequent_patterns/apriori/) dataset = [['Milk', 'Onion', 'Nutmeg', 'Kidney Beans', 'Eggs', 'Yogurt'], ['Dill', 'Onion', 'Nutmeg', 'Kidney…
kdas
  • 602
  • 2
  • 7
  • 22
4
votes
5 answers

Immutable Dictionary with key-object pairs Python

I have a dictionary filled with key-object pairs. I want to make the dictionary immutable and I thought the best/easiest way is to cast it to a frozenset but frozenset(dict) and also tuple(dict) only stores the keys. Using frozenset(dict.items()) I…
ThunderM
  • 41
  • 1
  • 7
1
2 3 4 5 6