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 7751845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:39:06+00:00 2026-06-01T11:39:06+00:00

I am new to coding python. I am hoping to modify this code to

  • 0

I am new to coding python. I am hoping to modify this code to develop a bipartite two mode version. It is code from networkx used to make a geometrci random graph. I have got to grips with most of this function but I am having trouble understanding exactly what lines 94 to 99 are doing. I understand while, zip and nodes.pop() but other parts are confusing for a newbie. Can anyone help with an explanation for what this part of the code is doing more than the general # description given ?

G=nx.Graph()
G.name="Random Geometric Graph"
G.add_nodes_from(range(n)) 
if pos is None:
    # random positions
    for n in G:
        G.node[n]['pos']=[random.random() for i in range(0,dim)]
else:
    nx.set_node_attributes(G,'pos',pos) 
# connect nodes within "radius" of each other
# n^2 algorithm, could use a k-d tree implementation
nodes = G.nodes(data=True)
while nodes:             #line94
    u,du = nodes.pop()
    pu = du['pos']
    for v,dv in nodes:
        pv = dv['pos']
        d = sum(((a-b)**2 for a,b in zip(pu,pv))) #line99
        if d <= radius**2:
            G.add_edge(u,v)
return G
  • 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-06-01T11:39:08+00:00Added an answer on June 1, 2026 at 11:39 am
    nodes = [some list]
    while nodes:
      a = nodes.pop()
      for b in nodes:
        # do something
    

    This piece of code is a quite frequently seen idiom to combine every node with every other node exactly once (so the order of a and b shouldn’t matter for the operation performed in the # do something part).

    It works because the empty list is considered a falsy value in the condition of the while loop, while non-empty lists are considered boolean true.

    d = sum(((a-b)**2 for a,b in zip(pu,pv)))
    

    This line computes the square of the Euclidean distance of the two vectors pu and pv. This is best demonstrated by taking it apart:

    >>> pu = (6,6,6)
    >>> pv = (1,3,7)
    >>> zip(pu, pv)
    [(6, 1), (6, 3), (6, 7)]
    >>> [(a-b) for a,b in zip(pu, pv)]
    [5, 3, -1]
    >>> [(a-b)**2 for a,b in zip(pu, pv)]
    [25, 9, 1]
    >>> sum((a-b)**2 for a,b in zip(pu, pv))
    35
    

    In the last step, we don’t use a list comprehension any more, because we don’t need a list. sum just needs the values in some iterable form, so we use a generator expression instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to C# coming from a python background. I've had a hard time
I'm somewhat new to C++, coming from python. One piece of functionality I really
I'm very new to Python (I'm coming from a JAVA background) and I'm wondering
I'm new to Python, coming from a C# background and I'm trying to get
here is my code #!/usr/bin/python # -*- coding:utf-8 -*- import sys import pysvn def
I have the following python modules. Sorry if the code is ugly. This is
I'm pretty new to Python, and I want to develop my first serious open
I'm leaning python and tried this code to test my 1st bit of OOP
I learned from the websitethat I should add the code declaration in python when
I am currently learning c++.My first language was python.I am used to coding in

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.