Python Forum
Randomise network while maintaining topology of nodes using edge-swap approach
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Randomise network while maintaining topology of nodes using edge-swap approach
#4
I would do your zip differently:

paired_lists = zip(list_of_edges[::2], list_of_edges[1::2])
Mainly because the above version works in Python 3.x, but whatever.

As to odd length lists of edges, I would just have an if clause at the end of the inner loop, and swap the extra item with the last item in the swapped list:

if len(list_of_edges) % 2:
    pair1 = pair_lambda(list_of_edges[-1])
    pair2 = pair_lambda(new_list.pop())
    # swap pairs and append to new_list
I wouldn't use lambdas for your functions, I would define actual functions. Lambdas are for one off functions, which yours are not. Also, since you are doing the swapping in the inner loop and in the if clause, I would make a function for that.

This works, my main concern is that it may not generate all possible reorderings with the same probability. Of course, if your list of edges has 200,000 items, that's probably not possible without being able to generate true random numbers.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
RE: Randomise network while maintaining topology of nodes using edge-swap approach - by ichabod801 - May-25-2017, 10:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Advice needed on how to approach this problem... sawtooth500 1 243 Apr-06-2024, 01:55 PM
Last Post: snippsat
  How can I rearrange df as the nodes index in pytorch geometric manner? uqlsmey 0 506 Jul-31-2023, 11:28 AM
Last Post: uqlsmey
  How to swap two numbers in fields in python Joni_Engr 5 1,836 Jan-11-2022, 09:43 AM
Last Post: menator01
  Python Networkx: Visualize an edge weight with a bubble/circle uvw 0 1,988 Sep-01-2021, 06:26 AM
Last Post: uvw
  Sound Approach for Running a Shell Command? matt_the_hall 8 3,330 Dec-14-2020, 02:52 PM
Last Post: matt_the_hall
  Need feedback on my approach for python dashboard for Asana pashtett 0 1,315 Nov-24-2020, 11:51 AM
Last Post: pashtett
  Swap key and value of a dictionary - and sort it Omid 4 2,819 Oct-28-2020, 01:24 PM
Last Post: Omid
Photo Setting Marker Edge Color In Plotting JoeDainton123 0 2,699 Oct-26-2020, 10:48 PM
Last Post: JoeDainton123
  Python3 binary tree not replacing parent nodes with child nodes Aspect11 0 1,760 Sep-23-2020, 02:22 PM
Last Post: Aspect11
  read individual nodes from an xml url using pandas mattkaplan27 5 2,922 Jul-05-2020, 10:06 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020