TypeError: unhashable type: 'list' in constructing poset

asked 2020-10-08 01:21:09 -0500

GA3165 gravatar image

updated 2020-10-08 21:53:47 -0500

I have the following code to generate all multiset partitions of a given multiset (list).

k0 = 2
k1 = 1
k2 = 1
#k3 = 1
l = k0+k1+k2 #+k3 

L = [ ]
for i in range(k0) :
    L.append(0)
for i in range(k1) :
    L.append(1)
for i in range(k2) :
    L.append(2)
print L
#L = [0,1,2,3]
LL = list(range(len(L)))
print LL
P = SetPartitions(LL)
P = list(P)

J = []
for p in P :
    J.append([])
    aa = P.index(p)
    for i in p :
        J[aa].append(list(i))
#print J    


JJ =[]
for j in J :
    JJ.append([])
    bb = J.index(j)
    for i in j :
        JJ[bb].append([])
        aa = j.index(i)
        for k in i :
            #print aa
            JJ[bb][aa].append(L[k])
print JJ

The above code gives me the following output

[0, 0, 1, 2]
[0, 1, 2, 3]
[[[0, 0, 1, 2]], [[0], [0, 1, 2]], [[0, 1, 2], [0]], [[0, 0, 2], [1]], [[0, 0, 1], [2]], [[0, 0], [1, 2]], [[0, 1], [0, 2]], [[0, 2], [0, 1]], [[0], [0], [1, 2]], [[0], [0, 2], [1]], [[0], [0, 1], [2]], [[0, 2], [0], [1]], [[0, 1], [0], [2]], [[0, 0], [1], [2]], [[0], [0], [1], [2]]]

I have the following code to generate a poset out of JJ in which the order is given by the refinement.

sage: elms = JJ
sage: def fcn(A, B):
....:     if len(A) != len(B) + 1:
....:         return False
....:     for a in A:
....:         if not any(set(a).issubset(b) for b in B):
....:             return False
....:     return True
sage: Poset((elms, fcn), cover_relations=True)

I usually work with sets and my input JJ is usually a set partition and the program works well.

Now, I am working with multisets (like [0,1,1,2,2,2,2]) and multi partitions (like [[0],[1,2],[1,2],[2,2]). By a multi-partition, I mean a partition in which each part is a multiset and parts can be repeated in a partition. So basically it is a list of lists. We cannot implement this using sets. I have codes given above to generate all such partitions in sage using lists.

Now, the set of all multi partitions JJ (which is implements as a list) of a multiset (which is also implemented as a list) has to be fed as an input to the above code to generate the poset. But the above code throws the error

 ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-42-993ec557f50c> in <module>()
      7             return False
      8     return True
----> 9 Poset((elms, fcn), cover_relations=True)

/Applications/SageMath-8.1.app/Contents/Resources/sage/local/lib/python2.7/site-packages/sage/combinat/posets/posets.pyc in Poset(data, element_labels, cover_relations, linear_extension, category, facade, key)
    674                         raise TypeError("not a list of relations")
    675             D = DiGraph()
--> 676             D.add_vertices(elements)
    677             D.add_edges(relations, loops=False)
    678         elif len(data) > 2:

/Applications/SageMath-8.1.app/Contents/Resources/sage/local/lib/python2.7/site-packages/sage/graphs/generic_graph.pyc in add_vertices(self, vertices)
   9602 
   9603         """
-> 9604         return self._backend.add_vertices(vertices)
   9605 
   9606     def delete_vertex(self, vertex, in_order=False):

/Applications/SageMath-8.1.app/Contents/Resources/sage/src/sage/graphs/base/c_graph.pyx in sage.graphs.base.c_graph.CGraphBackend.add_vertices (build/cythonized/sage/graphs/base/c_graph.c:14020)()
   1504         for v in vertices:
   1505             if v is not None:
-> 1506                 self.add_vertex(v)
   1507             else:
   1508                 nones += 1

/Applications/SageMath-8.1.app/Contents/Resources/sage/src/sage/graphs/base/c_graph.pyx in sage.graphs.base.c_graph.CGraphBackend.add_vertex (build/cythonized/sage/graphs/base/c_graph.c:13856)()
   1454             retval = name
   1455 
-> 1456         self.check_labelled_vertex(name,
   1457                      (self._directed and
   1458                       self._cg_rev is not None)) # this will add the vertex

/Applications/SageMath-8.1.app/Contents/Resources/sage/src/sage/graphs/base/c_graph.pyx in sage.graphs.base.c_graph.CGraphBackend.check_labelled_vertex (build/cythonized/sage/graphs/base/c_graph.c:12310)()
   1153         cdef CGraph G_rev = self._cg_rev
   1154 
-> 1155         cdef int u_int = self.get_vertex(u)
   1156         if u_int != -1:
   1157             if not bitset_in(G.active_vertices, u_int):

/Applications/SageMath-8.1.app/Contents/Resources/sage/src/sage/graphs/base/c_graph.pyx in sage.graphs.base.c_graph.CGraphBackend.get_vertex (build/cythonized/sage/graphs/base/c_graph.c:11902)()
   1120         cdef CGraph G = self._cg
   1121         cdef long u_long
-> 1122         if u in vertex_ints:
   1123             return vertex_ints[u]
   1124         try:

TypeError: unhashable type: 'list'

How to overcome this issue? Kindly help me with this. Thank you.

edit retag flag offensive close merge delete

Comments

Please provide a sequence of commands one can run to get the same error.

Could not obtain the same error, trying

  • with M = [0, 1, 1, 2, 2, 2, 2] followed by the code in the question
  • with M = [[0], [1, 2], [1, 2], [2, 2]] followed by the code in the question
slelievre gravatar imageslelievre ( 2020-10-08 09:24:04 -0500 )edit

@slelievre I have added the full code. Kindly check. Thank you. I directly feed M = [[0], [1, 2], [1, 2], [2, 2]] but it is throwing the same error message.

GA3165 gravatar imageGA3165 ( 2020-10-08 19:49:00 -0500 )edit
1

Suggestion : use tuples (and sort them) instead of lists.

FrédéricC gravatar imageFrédéricC ( 2020-10-09 08:59:24 -0500 )edit