[PYTHON] [NetworkX] I want to search for nodes with specific attributes

Overview

I'm using NetworkX in my research, but I wanted to search for a node with a specific attribute, and as a result of searching for such a method, I made it.

code

find_specific_attribute_node.py


import networkx as nx

def find_specific_attribute_node(G, attr, value):

    result = []

    d = nx.get_node_attributes(G, attr)

    for key, v in d.items():
        if(v == value):
            result.append(key)

    return result

Operation example

Let's run the following test program.

find_node_test_for_qiita.py


import networkx as nx
from find_specific_attribute_node import find_specific_attribute_node as find_nodes
if __name__ == '__main__':

    Graph = nx.DiGraph()

    Graph.add_node('a', color = 'blue')
    Graph.add_node('b', color = 'red')
    Graph.add_node('c', color = 'blue')
    Graph.add_node('d', color = 'red')
    Graph.add_node('e', color = 'blue')
    Graph.add_node('f', color = 'red')

    print(find_nodes(Graph, 'color', 'blue'))

Below is the execution result.

Execution result


['a', 'e', 'c']

You can see that a list consisting of node names with blue set in the color attribute has been acquired.

Description

It's a very suitable one, so it has the following three arguments.

--G: Graph to be searched --attr: Attribute name you want to search --value: the value of the attr you want to find

The return value is a list of found node names.

in conclusion

Since it seems to be very demanding, there may actually be a method with a function similar to NetworkX. Please let me know.

Also, I haven't studied the naming conventions such as methods properly, so I'd be happy if you could tell me if there is something like "I should give it such a name" ...

Recommended Posts

[NetworkX] I want to search for nodes with specific attributes
I want to do ○○ with Pandas
I want to debug with Python
I want to do a full text search with elasticsearch + python
For the time being, I want to convert files with ffmpeg !!
I want to detect objects with OpenCV
I want to blog with Jupyter Notebook
I want to pip install with PythonAnywhere
I want to analyze logs with Python
I want to play with aws with python
I want to use MATLAB feval with python
I want to analyze songs with Spotify API 2
I want to display multiple images with matplotlib.
I want to make a game with Python
I want to be an OREMO with setParam!
I want to analyze songs with Spotify API 1
I want to use Temporary Directory with Python2
I don't want to use -inf with np.log
#Unresolved I want to compile gobject-introspection with Python3
I want to use ip vrf with SONiC
I want to solve APG4b with Python (Chapter 2)
I want to start over with Django's Migrate
I want to write to a file with Python
I want to convert an image to WebP with lollipop
I want to handle optimization with python and cplex
I want to climb a mountain with reinforcement learning
I want to inherit to the back with python dataclass
I want to work with a robot in python.
I want to split a character string with hiragana
I want to AWS Lambda with Python on Mac!
I want to manually create a legend with matplotlib
[TensorFlow] I want to process windows with Ragged Tensor
For those who want to write Python with vim
[ML Ops] I want to do multi-project with Python
I want to run a quantum computer with Python
I want to bind a local variable with lambda
[Python] I want to use only index when looping a list with a for statement
[Linux] When you want to search for a specific character string from multiple files
[TensorFlow] I want to master the indexing for Ragged Tensor
I want to be able to analyze data with Python (Part 3)
I want to remove Python's Unresolved Import Warning with vsCode
I want to use R functions easily with ipython notebook
I want to specify another version of Python with pyvenv
I want to exchange gifts even for myself! [Christmas hackathon]
I want to move selenium for the time being [for mac]
I want to be able to analyze data with Python (Part 1)
I want to make a blog editor with django admin
I want to start a jupyter environment with one command
I want to make a click macro with pyautogui (desire)
I want to change the Japanese flag to the Palau flag with Numpy
I want to be able to analyze data with Python (Part 4)
I want to solve Sudoku (Sudoku)
For those who want to start machine learning with TensorFlow2
I want to color black-and-white photos of memories with GAN
I want to be able to analyze data with Python (Part 2)
I want to automatically attend online classes with Python + Selenium!
I want to make a click macro with pyautogui (outlook)
[Python] I want to use the -h option with argparse
I want to use a virtual environment with jupyter notebook!
I want to install a package from requirements.txt with poetry
I can't search with # google-map. ..