Python progression path - From apprentice to guru
I thought the process of Python mastery went something like:
- Discover list comprehensions
- Discover generators
- Incorporate map, reduce, filter, iter, range, xrange often into your code
- Discover Decorators
- Write recursive functions, a lot
- Discover itertools and functools
- Read Real World Haskell (read free online)
- Rewrite all your old Python code with tons of higher order functions, recursion, and whatnot.
- Annoy your cubicle mates every time they present you with a Python class. Claim it could be "better" implemented as a dictionary plus some functions. Embrace functional programming.
- Rediscover the Strategy pattern and then all those things from imperative code you tried so hard to forget after Haskell.
- Find a balance.
Find Networkx path by a sequence of nodes
There's some reading between the lines that you've left for us. For example, are you specifically looking for paths that end in node 7 in this case, or are you just looking for any path that has nowhere else to go (i.e. ends in a node of outdegree zero)? If the path contains a cycle, do we include paths that go around the cycle before ending?
My guess is that what you're really interested in is the set of all simple paths from a given node that end in a node with outdegree zero. With that in mind, here's some code you might find useful.
start = 1
ends = [n for n in DG.nodes if DG.out_degree[n] == 0]
paths = [p for e in ends
for p in nx.all_simple_paths(DG, source = start, target = e)]
print(paths)
Output: [[1, 2, 4, 5, 6, 7], [1, 2, 3, 6, 7]]
If you want paths that start from 1 and end in any other node, you could do the following.
start = 1
paths = [p for e in DG.nodes
for p in nx.all_simple_paths(DG, source = start, target = e)]
print(paths)
Output: [[1, 2], [1, 2, 4], [1, 2, 3], [1, 2, 4, 5, 6], [1, 2, 3, 6], [1, 2, 4, 5, 6, 7], [1, 2, 3, 6, 7], [1, 2, 4, 5]]
.
If you like, you can take this output and filter down to the paths that start with any 3 nodes. For example,
print([p for p in paths if p[:3] == [1,2,3]])
print([p for p in paths if p[:3] == [1,2,4]])
results in the outputs [[1, 2, 3], [1, 2, 3, 6], [1, 2, 3, 6, 7]]
and [[1, 2, 4], [1, 2, 4, 5, 6], [1, 2, 4, 5, 6, 7], [1, 2, 4, 5]]
.
Alternatively, sorting paths
naturally organizes the paths according to their first nodes. In particular, sorted(paths)
is the list
[[1, 2],
[1, 2, 3],
[1, 2, 3, 6],
[1, 2, 3, 6, 7],
[1, 2, 4],
[1, 2, 4, 5],
[1, 2, 4, 5, 6],
[1, 2, 4, 5, 6, 7]]
which you could split into two pieces.
Related Topics
Pandas Concat Generates Nan Values
General Unicode/Utf-8 Support for CSV Files in Python 2.6
Prevent Plot from Showing in Jupyter Notebook
Printing Utf-8 in Python 3 Using Sublime Text 3
Matplotlib: Plotting Numerous Disconnected Line Segments with Different Colors
How to Remove Item from a Python List in a Loop
How to Execute a Command Prompt Command from Python
How Does Python Importing Exactly Work
How to Deal with Kivy Installing Error in Python 3.8
Hitting Maximum Recursion Depth Using Pickle/Cpickle
How Does Keras Calculate the Accuracy
What Is the Point of Setlevel in a Python Logging Handler
Calling Matlab Functions from Python
Image Segmentation Based on Edge Pixel Map
How to Sort and Remove Duplicates from Python List