Skip to main content

Thread: [Python] Problem with pushing two objects at a time to heapq


the way understand it, when push tuple list using heapq.heappush, python assumes element (number) @ index 0 priority of item. elements @ index 1 till-python-can-take-it, irrelevant.

if so, can't understand python3 complaining in here,

code:
code:
while < limit:  				cost = neighbor_nodes[i].__manhattan_distance() + depth  				candidate_tuple = (cost, neighbor_nodes[i], current_node[puzzle_index])  				  				if candidate_tuple in traversed:  					pass  				else:  					print("heapq.heappush(", choice_space, ",", candidate_tuple, ")")  					print(type(choice_space))  					print(type(candidate_tuple))  					heapq.heappush(choice_space, candidate_tuple) #mark line!  					print("valid push!")  				  				i += 1
(of course, that's snippet. post more needed.)

sorry spacing. assume works.

can produce trace like

code:
heapq.heappush( [] , (28, <npuzzle_oo.npuzzle object @ 0xb761b48c>, <npuzzle_oo.npuzzle object @ 0xb762546c>) )  <class 'list'>  <class 'tuple'>  valid push!  heapq.heappush( [(28, <npuzzle_oo.npuzzle object @ 0xb761b48c>, <npuzzle_oo.npuzzle object @ 0xb762546c>)] , (26, <npuzzle_oo.npuzzle object @ 0xb75d91cc>, <npuzzle_oo.npuzzle object @ 0xb762546c>) )  <class 'list'>  <class 'tuple'>  valid push!  heapq.heappush( [(26, <npuzzle_oo.npuzzle object @ 0xb75d91cc>, <npuzzle_oo.npuzzle object @ 0xb762546c>), (28, <npuzzle_oo.npuzzle object @ 0xb761b48c>, <npuzzle_oo.npuzzle object @ 0xb762546c>)] , (27, <npuzzle_oo.npuzzle object @ 0xb761b4ac>, <npuzzle_oo.npuzzle object @ 0xb762546c>) )  <class 'list'>  <class 'tuple'>  valid push!  heapq.heappush( [(26, <npuzzle_oo.npuzzle object @ 0xb75d91cc>, <npuzzle_oo.npuzzle object @ 0xb762546c>), (28, <npuzzle_oo.npuzzle object @ 0xb761b48c>, <npuzzle_oo.npuzzle object @ 0xb762546c>), (27, <npuzzle_oo.npuzzle object @ 0xb761b4ac>, <npuzzle_oo.npuzzle object @ 0xb762546c>)] , (28, <npuzzle_oo.npuzzle object @ 0xb761b46c>, <npuzzle_oo.npuzzle object @ 0xb762546c>) )  <class 'list'>  <class 'tuple'>
and error message,
code:
file "filepath/filename.py", line 416, in solve      heapq.heappush(choice_space, candidate_tuple)  typeerror: unorderable types: npuzzle() < npuzzle()
(line 416 line commented "mark line!")

i'm missing? why successful in pushing first few times throws error?

thanks!

found answer own question. seems that, when keys equal, python resorts comparing values call __lt__.

http://stackoverflow.com/questions/3...ific-attribute


Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk [SOLVED] [Python] Problem with pushing two objects at a time to heapq


Ubuntu

Comments