how checking if there is a value in a heapq

harry

I am working heapq package in order to work with graphs.

Let's suppose a list " heap ", filed by 2 tuples a and b representing ( distance, node )

import heapq

heap = []
a = (321,4)
b = (258,3)
heapq.heappush(heap,a)
heapq.heappush(heap,b)

Is there any way to check if node 4 is in the heap list ? and if yes, how can I get its distance ?

DirtyBit

Using any:

import heapq

heap = []
a = (321,4)
b = (258,3)
heapq.heappush(heap,a)
heapq.heappush(heap,b)
node = 4
if any(node in d for d in heap):
     print("The Distance of the node {} is {}".format(node, [x[0] for x in heap if x[1] == node]))

OUTPUT:

The Distance of the node 4 is [321]

OR:

print("The Distance of the node {} is {}".format(node, str([x[0] for x in heap if x[1] == node]).strip("[]")))

OUTPUT:

The Distance of the node 4 is 321

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Not checking close()'s return value: how serious, really?

How to remove element from ArrayList by checking its value?

Comparing two dictionaries and checking how many (key, value) pairs are equal

How to remove double checking when setting a value in a multithreaded environment

Setter - Checking the value

Openpyxl: How to copy a row after checking if a cell contains specific value

AngularJS - How to set the value of the checkbox as true by checking another checkbox?

How to extract values from top level, by checking nested value?

How to pass columns as value in UDF in Spark Scala for checking condition

how to avoid using _siftup or _siftdown in heapq

How to check if string is valid by checking if its value is in array

How to create groups checking elements by attribute value

Checking array for a value

How to create binary pandas dataframe by checking value is equal to mode or not

How to fetch the value in dictionary by checking the key mapped in the dict

How does heapq resolve equal values?

How to use heapq module

How to assign a value to sqlparameter NULL values without checking for null or 0?

How to empty INPUT after checking for numeric value?

How to checking value from dynamic substr then can be query in mysql?

Check if heapq contains value

SQL how to implement if and else by checking column value

How to get value of array by checking its value

How to add a timeout to Runtime.exec() but checking exit value?

Python, heapq, How to efficiently modify the minimum element in heapq?

How can i simplify checking if a value exist in Json doc

How to get highest value for each group by checking with two columns value

How to keep checking a value until it changes without using infinite loop?

How to add list to heapq in python

TOP Ranking

HotTag

Archive