How to use the set() function in a list of lists or in a list of tuples?

Asked

Viewed 251 times

0

I have a list of tuples generated with the Combinations function of the itertools module:

from itertools import combinations as comb

A = range(x,xx)
B = list(comb(A,x)

I generated this list of tuples with the intention of then making an intersection with some variables and for that I made a list:

V1= x, x,x,x
V2= x,x,x,x,x
V3= x,x,x,x,x
V4= x,x,x,x,x

inter=[len(set(x) & set (y)) for x in B for y in [V1,V2,V3,V4]]

Then I sliced the result of the intersection with another list:

fatia = [inter[i:i+4] for i in range(0,len(inter),4)]

I got the desired result but with a failure, when I use the function set() or len(set(fatia)) get the following message: unhashable type: ' list '

This is depressing because I created the program in order to know the exact number of patterns generated by the intersection and it was not easy for me to learn these commands.

Could someone help me solve this problem?

  • The mistake happens because you are trying to build a set of list, but list can’t be hashish (that I imagine the set need to do underneath). set([[]]) generates the same error. But I didn’t quite understand what you want, is to count the intersections of what the combinations returns and what, exactly?

  • Hello, sorry for the delay. Yes, I want to count how many intersection patterns there are in the number of combinations generated to complete my analysis. Thank you for trying to help.

  • The goal of the program is to achieve the intersection between V1, V2, V3 and V4, to then calculate the amount of elements at the intersection? What do you mean by "patterns generated by the intersection"?

  • If you can, edit to put examples of possible values in x in V1= x, x,x,x, in A = range(x,xx), still not understood 100% the purpose of the code, as well as the result you expect to get with the program

  • @danielbb, thanks for coming here on my question. I want to be able to use the set() function in the list list generated by the list with "slice".

  • The values are not important, you can put any value there, what I really wanted was to be able to use the function set() to know the exact number of existing patterns in the list of tuples generated by the simple combination formula (Combinations).

  • The exact number of patterns generated by the existing intersection within the universe generated by the Combinations function.

  • 1

    I get it. And the sole purpose of set in len(set(fatia)) is to eliminate duplicates, right?

  • @danielbb, yes exactly that!

  • @danielbb, remove duplicates to know the exact number of patterns.

Show 5 more comments

1 answer

1


Observe the code

a = [0, 5, 2]
b = [7, 3, 2]
c = [1, 2, 3]

listaDeListas = [a, a, b, c, a, c, a, c, b]

print(len(set(listaDeListas)))

Its result is

Traceback (most recent call last):
  File "interseção.py", line 9, in <module>
    len(set(listaDeListas))
TypeError: unhashable type: 'list'

This is because lists, like many of the mutable objects, are not "hashable"[1], which is a requirement to be an item of an object of the type set[2].

A possible solution would be to convert the items to an immutable object. The immutable object "closest" to a list, is a tuple:

a = [0, 5, 2]
b = [7, 3, 2]
c = [1, 2, 3]

listaDeListas = [a, a, b, c, a, c, a, c, b]
listaDeTuplas = [tuple(i) for i in listaDeListas]
print(len(set(listaDeTuplas)))

returns

3

Note that in this example, items such as a = [0, 1] and b = [1, 0] are considered distinct. Depending on the situation, this may be inconvenient.


[1] I call type "hasheable" a type that implements the function __hash__.

[2] Documentation of set: The classes of sets (set) are implemented using dictionaries(dict). Therefore, the requirements for an item to belong to a set are the same for dictionary keys; they must implement __hash__ and __eq__

  • Thank you @danielbb !! That’s exactly what it was. I need to study more. Thank you!

  • When I have a reputation I’ll see you on chat. See you around.

  • I’m happy to help. Good studies!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.