Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set of sets and "in" operator [duplicate]

Tags:

python

set

I was doing some coding exercises and I ended up using a set of frozensets. Here is the code:

cities = 4
roads = [[0, 1], [1, 2], [2, 0]]
roads = set([frozenset(road) for road in roads])
    
output = []
    
for i in range(cities-1):
    for j in range(i+1, cities):
        if set([i,j]) not in roads:
            output.append([i,j])

As you can see, the if in the nested for tests for the presence of the set in the set of sets. However, it was my understanding that in this case, hashables need to be used with the in operator.

If I replace set([i,j]) with [i,j], I do get the following error:

TypeError: unhashable type: 'list'

So, here is my question: why does it work with the set, which is not (as far as I know) hashable and not with the list? Should it not also throw an error, what am I missing?

like image 540
Ben Avatar asked Jul 17 '21 22:07

Ben


People also ask

What can be said about duplicate values and set operators?

When used with two SELECT statements, the UNION set operator returns the results of both queries. However,if there are any duplicates, they are removed, and the duplicated record is listed only once.To include duplicates in the results,use the UNION ALL set operator.

What are the 4 set operators?

Set operators are used to join the results of two (or more) SELECT statements. The SET operators available in Oracle 11g are UNION,UNION ALL,INTERSECT,and MINUS.

Can sets have duplicates Python?

A set is an unordered collection of items. Every set element is unique (no duplicates) and must be immutable (cannot be changed).

What is the purpose of the set operator?

The SET operator allows the tables of a database to be treated as objects in a set when performing a query. In mathematics, the various operations on sets using mathematical operators, must act on at least two different sets to produce results. Naturally, when applied to databases, SET operations must work on two...

What are the operators available in set and frozenset?

Sets and frozensets support the following operators: key in s: It is used to check that the given key is in the set or not. key not in s: it returns True if the key is not in the set.

What is a set in SQL?

The mathematical concept of a set applies to SQL databases too. All SQL implementations are able to treat tables as sets and to apply SET operators on them. Similarities between sets and tables and how SET operators act on tables will be discussed. Updated: 01/23/2020 What is the SET Operator?

What are the rules for set operators in SQL?

Operators covered under SET operators are: There are certain rules which must be followed to perform operations using SET operators in SQL. Rules are as follows: The number and order of columns must be the same. Data types must be compatible.


2 Answers

From my reading of the CPython source it appears that the test for contains checks if the key is found in the set; if not, and if the key is a set object, an attempt is made to convert the key to a frozenset, and then that key is tested. The same behavior exists for operations like remove, as seen here:

>>> s = set([frozenset([1,2])])
>>> s
{frozenset({1, 2})}
>>> s.remove(set([1,2]))
>>> s
set()

The code in question in the interpreter is the set_contains() function in Objects/setobject.c.

like image 187
sj95126 Avatar answered Oct 17 '22 21:10

sj95126


Nevermind, found the answer in the documentation, for anyone wondering:

Note, the elem argument to the __contains__(), remove(), and discard() methods may be a set. To support searching for an equivalent frozenset, a temporary one is created from elem.

like image 27
Ben Avatar answered Oct 17 '22 22:10

Ben