Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4079850
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:51:16+00:00 2026-05-20T17:51:16+00:00

I am using the NetworkX graph library for Python. At some point in my

  • 0

I am using the NetworkX graph library for Python. At some point in my program I would like to “consolidate” my nodeIDs into a sequence of numbers. Here’s my naive approach:

start = 1 # could be anything
for i, n in enumerate(g.nodes()):
    if i+start == n:
        continue
    g.add_node(i+start, attr_dict=g.node[n])
    g.add_edges_from([(i+start, v, g[n][v]) for v in g.neighbors(n)])
    g.remove_node(n)

Is there a faster way than this exhaustive copy of all the neighbors? For example, I tried g[i+start] = g[n], but that is forbidden.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:51:17+00:00Added an answer on May 20, 2026 at 5:51 pm

    Would this work?

    http://networkx.github.io/documentation/latest/reference/generated/networkx.relabel.convert_node_labels_to_integers.html

    import networkx as nx
    G = nx.Graph()
    G.add_node(1)
    G.add_nodes_from('spam')
    print G.nodes()
    

    returns:

    ['a', 1, 's', 'm', 'p']
    

    now:

    start = 1
    G = nx.convert_node_labels_to_integers(G,first_label=start)
    print G.nodes()
    

    returns:

    [1, 2, 3, 4, 5]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Python's Networkx library, I created an undirected graph to represent a relationship network
The Python library NetworkX has a fancy animated graph on their home page .
I have a program written in python using the module networkx to create a
I am using networkx (a python graph-drawing package) http://networkx.lanl.gov/index.html for one of my project.
Possible Duplicate: [python]: path between two nodes Can anyone point me to some resources
I am using boost graph library for solving a network graph problem. I have
Long story short, I'm creating a graph using networkx and the lower the weights
I'm hooked on using Python and NetworkX for analyzing graphs and as I learn
I'm using Ruby to build gexf-formatted XML structure that would represent a network graph.
I'm working on some code for a directed graph in NetworkX, and have hit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.