Questions tagged [frozenset]

66 questions
43
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
  • 2,854
  • 4
  • 21
  • 32
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,365
  • 3
  • 14
  • 24
15
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,691
  • 1
  • 15
  • 20
14
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
  • 22,482
  • 26
  • 85
  • 135
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
  • 2,771
  • 14
  • 24
5
votes
3 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…
mlRocks
  • 1,414
  • 13
  • 26
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,489
  • 1
  • 26
  • 38
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
  • 357
  • 3
  • 16
4
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
  • 447
  • 3
  • 8
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
  • 462
  • 4
  • 16
3
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?
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
  • 427
  • 1
  • 4
  • 14
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
  • 2
  • 12
3
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
  • 31
  • 1
  • 7
1
2 3 4 5