Note

This is the documentation for the current state of the development branch of rustworkx. The documentation or APIs here can change prior to being released.

rustworkx.PyGraph.filter_nodes#

PyGraph.filter_nodes(filter_function)#

Filters a graph’s nodes by some criteria conditioned on a node’s data payload and returns those nodes’ indices.

This function takes in a function as an argument. This filter function will be passed in a node’s data payload and is required to return a boolean value stating whether the node’s data payload fits some criteria.

For example:

from rustworkx import PyGraph

graph = PyGraph()
graph.add_nodes_from(list(range(5)))

def my_filter_function(node):
    return node > 2

indices = graph.filter_nodes(my_filter_function)
assert indices == [3, 4]
Parameters:

filter_function – Function with which to filter nodes

Returns:

The node indices that match the filter

Return type:

NodeIndices