create a list but getting a string? - python

s = """
1:A,B,C,D;E,F
2:G,H;J,K
&:L,M,N
"""
def read_nodes(gfile):
for line in gfile.split():
nodes = line.split(":")[1].replace(';',',').split(',')
for node in nodes:
print node
print read_nodes(s)
I am expected to get ['A','B','C','D','E',.....'N'], but I get A B C D E .....N and it's not a list. I spent a lot of time debugging, but could not find the right way.

I believe this is what you're looking for:
s = """
1:A,B,C,D;E,F
2:G,H;J,K
&:L,M,N
"""
def read_nodes(gfile):
nodes = [line.split(":")[1].replace(';',',').split(',') for line in gfile.split()]
nodes = [n for l in nodes for n in l]
return nodes
print read_nodes(s) # prints: ['A','B','C','D','E',.....'N']
What you were doing wrong is that for each sub-list you create, your were iterating over that sub-list and printing out the contents.
The code above uses list comprehension to first iterate over the gfile and create a list of lists. The list is then flattened with the second line. Afterwards, the flatten list is returned.
If you still want to do it your way, Then you need a local variable to store the contents of each sub-list in, and then return that variable:
s = """
1:A,B,C,D;E,F
2:G,H;J,K
&:L,M,N
"""
def read_nodes(gfile):
all_nodes = []
for line in gfile.split():
nodes = line.split(":")[1].replace(';',',').split(',')
all_nodes.extend(nodes)
return all_nodes
print read_nodes(s)

Each line you read will create a new list called nodes. You need to create a list outside this loop and store all the nodes.
s = """
1:A,B,C,D;E,F
2:G,H;J,K
&:L,M,N
"""
def read_nodes(gfile):
allNodes = []
for line in gfile.split():
nodes =line.split(":")[1].replace(';',',').split(',')
for node in nodes:
allNodes.append(node)
return allNodes
print read_nodes(s)

Not quite sure what you are ultimately trying to accomplish but this will print what you say you are expecting:
s = """
1:A,B,C,D;E,F
2:G,H;J,K
&:L,M,N
"""
def read_nodes(gfile):
nodes = []
for line in gfile.split():
nodes += line.split(":")[1].replace(';',',').split(',')
return nodes
print read_nodes(s)

Add the following code so that the output is
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N']
//Code to be added
nodes_list = []
def read_nodes(gfile):
for line in gfile.split():
nodes =line.split(":")[1].replace(';',',').split(',')
nodes_list.extend(nodes)
print nodes_list
print read_nodes(s)

Related

Pointer error in Directed Graph in Python

I'm trying to implement a Directed graph in python by creating a class Vertex. A vertex has the name and the adjacent as a list containing another instance of class Vertex . below is the code.
The problem is that when I assign node b as the adjacent node from node a. the pointer of node b is assigned to both adjacent of a and b which is not my intention.
I also have the picture debug mode below the code showing that the pointer of node b is also assigned to itself.
how to fix it?
class Vertex:
def __init__(self, name, adjacent=[]):
self.name = name
self.adjacent = adjacent
def add_adjacent(self, vertex):
self.adjacent.append(vertex)
class Graph:
# directed graph
def __init__(self, edge_list):
vertices = {}
for o, d in edge_list:
# print(o, d)
if o not in vertices:
v = Vertex(o)
vertices[o] = v
else:
v = vertices[o]
if d not in vertices:
u = Vertex(d)
vertices[d] = u
else:
u = vertices[d]
if u not in v.adjacent:
print(v.name, ' adds ', u.name)
v.add_adjacent(u)
self.vertices = vertices
def get_vertex_names(self):
return list(self.vertices.keys())
def get_adjacent(self, vertex):
return self.vertices[vertex].adjacent
# test Vertex
edges = [
['a', 'b'],
['a', 'c'],
['a', 'd'],
['b', 'c'],
['c', 'b'],
]
g = Graph(edges)
# g.vertices
error can be seen in debug mode
You wrote this:
def __init__(self, name, adjacent=[]):
There is a subtle "gotcha",
summarized by a simple rule,
Avoid using mutable container as a default arg.
You want this:
def __init__(self, name, adjacent=None):
self.name = name
self.adjacent = adjacent or []
In the original, there is a single list
for all vertices (since caller always lets it default).
That list was created at def time.
In contrast, the or [] clause
constructs a new list at execution time
each time through, rather than once during definition.
https://dollardhingra.com/blog/python-mutable-default-arguments/
https://towardsdatascience.com/python-pitfall-mutable-default-arguments-9385e8265422

Merging overlapping (str) objects

The problem is the following :
I want to go from having this set
{'A/B', 'B/C', 'C/D', 'D/E', ..., 'U/V', 'V/W', ..., 'X/Y', ..., 'Z', ...}
to this set
{'A/B/C/D/E', ..., 'U/V/W', ..., 'X/Y', ..., 'Z', ...}
where the objects A, B, C ... are just strings of characters. The output solution should be independent of the order in which the objects appears (i.e. if you scramble the objects in the set, the solution should always be the same)
In other words I want to merge overlapping objects.
Inputs of the following form cannot happen :
{"A/B", "B/C", "B/D"}
{"A/B", "B/C", "C/A"}
There can be objects with no '/' in them.
Here is a partial solution I've come up with :
example={'A/B', 'B/C', 'C/D', 'D/E','U/V', 'V/W','X/Y'}
def ext_3plus(unit):
for couple in list(itertools.combinations(list(unit),2)):
if '/' in couple[0] and '/' in couple[1]:
if couple[0].split('/')[0]==couple[1].split('/')[1]:
unit.remove(couple[0])
unit.remove(couple[1])
unit.add(couple[1].split('/')[0]+'/'+couple[0])
if couple[0].split('/')[1]==couple[1].split('/')[0]:
unit.remove(couple[0])
unit.remove(couple[1])
unit.add(couple[0]+'/'+couple[1].split('/')[1])
else: #the input can contain object not having '/'
continue
There is two problems, first it does only one iteration,
the result on {'A/B', 'B/C', 'C/D', 'D/E','U/V', 'V/W','X/Y'}
is :
{'A/B/C', 'C/D/E', 'U/V/W', 'X/Y'}
Second, if I include objects containing no '/', the input being {'A/B', 'B/C', 'C/D', 'D/E','U/V', 'V/W','X/Y','Z'}, the result is different from the previous one :
{'A/B', 'B/C/D', 'D/E', 'U/V/W', 'X/Y', 'Z'}
So there should be a recursive call on the first iteration etc.
How should it be done ?
If I understood correctly this can be seen as a graph problem, and solve as such:
import networkx as nx
example = {'A/B', 'B/C', 'C/D', 'D/E', 'U/V', 'V/W', 'X/Y', "Z"}
# convert each string to a and edge
# each pattern to the side of / is a node
edges = [tuple(s.split("/")) for s in example if "/" in s]
nodes = [s for s in example if "/" not in s]
# create directed graph from edges
g = nx.from_edgelist(edges, create_using=nx.DiGraph)
g.add_nodes_from(nodes)
# find each path using topological sort
runs, current = [], []
for e in nx.topological_sort(g):
# start a new path each time a node with in-degree 0
# in-degree 0 means it is the start of a new path
if g.in_degree(e) == 0:
if current:
runs.append(current)
current = []
current.append(e)
if current:
runs.append(current)
# format the result
result = ["/".join(run) for run in runs]
print(result)
Output
['Z', 'U/V/W', 'X/Y', 'A/B/C/D/E']
If I'm not mistaken the overall complexity of this approach is O(n). More on topological sorting can be found here.
UPDATE
In networkx 2.6.4 use lexicographical_topological_sort
You can use a recursive generator function:
vals = ['A/B', 'B/C', 'C/D', 'D/E', 'U/V', 'V/W', 'X/Y']
data = [i.split('/') for i in vals]
def paths(d, c = [], s = []):
if not (k:=[b for a, b in data if a == d]):
yield c+[d]
if (t:=[a for a, b in data if a not in s+[d]]):
yield from paths(t[0], c = [], s=s+[d])
else:
yield from [j for i in k for j in paths(i, c=c+[d], s=s+[d])]
vals = list(paths(data[0][0]))
Output:
[['A', 'B', 'C', 'D', 'E'], ['U', 'V', 'W'], ['X', 'Y']]
It should be noted, however, that the solution above will only work on inputs that contain standard edge definitions. If the contents of vals can very in the number of items divided by the /, then you can use the solution below:
class Node:
def __init__(self, n, c = []):
self.n, self.c = n, c
def __contains__(self, e):
return e[0] == self.n or e[-1] == self.n or any(e in i for i in self.c)
def add_edge(self, e):
if self.n != e[0] and len(e) > 1 and (m:=[i for i in self.c if i.n == e[-1]]):
self.c = [i for i in self.c if i != m[0]]+[Node(e[0], [m[0]])]
elif self.n == e[0]:
if len(e) > 1 and not any(i.n == e[1] for i in self.c):
self.c = [*self.c, Node(e[1])]
elif (m:=[i for i in self.c if e in i]):
m[0].add_edge(e)
else:
self.c = [*self.c, Node(e[0], [] if len(e) == 1 else [Node(e[1])])]
vals = ['A/B/C', 'A/B', 'B/C', 'C/D', 'D/E', 'U/V', 'V/W', 'X/Y', 'K']
n = Node(None)
for i in vals:
k = i.split('/')
for j in range(len(k)):
n.add_edge(k[j:j+2])
def get_paths(n, c = []):
if not n.c:
yield c+[n.n]
else:
yield from [j for k in n.c for j in get_paths(k, c+[n.n])]
final_result = [i[1:] for i in get_paths(n)]
print(final_result)
Output:
[['A', 'B', 'C', 'D', 'E'], ['U', 'V', 'W'], ['X', 'Y'], ['K']]
With the trie-style approach of class Node, the order of the input (vals) does not matter (no sort is required) and input paths of any depth can be added.
It might not be the most efficient, but you could just repeat the loop until there's nothing modified.
def ext_3plus(unit):
while True:
oldlen = len(unit)
for couple in itertools.combinations(list(unit),2):
if '/' in couple[0] and '/' in couple[1]:
if couple[0].split('/')[0]==couple[1].split('/')
unit.remove(couple[0])
unit.remove(couple[1])
unit.add(couple[1].split('/')[0]+'/'+couple[0])
modified = True
if couple[0].split('/')[1]==couple[1].split('/')[0]
unit.remove(couple[0])
unit.remove(couple[1])
unit.add(couple[0]+'/'+couple[1].split('/')[1])
if len(unit) == oldlen:
# Nothing was merged, so we're done
break

Topological Sort Algorithm (DFS) Implementation in Python

I am new to python and algorithms. I have been trying to implement a topological sorting algorithm for a while but can't seem to create a structure that works. The functions I have made run on a graph represented in an adj list.
When I have a DFS, the nodes are discovered top down, and nodes that have been already visited and not processed again:
def DFS(location, graph, visited = None):
if visited == None:
visited = [False for i in range(len(graph))]
if visited[location] == True:
return
visited[location] = True
node_visited.append(location)
for node in graph[location]:
DFS(node, graph, visited)
return visited
When I am trying to build a topological sort algorithm, I create a new function which essentially checks the "availability" of that node to be added to the sorted list (ie: whether its neighbouring nodes have been visited already)
def availability(graph, node):
count = 0
for neighbour in graph[node]:
if neighbour in available_nodes:
count += 1
if count != 0:
return False
return True
However, my issue is that once I have visited the node path to get to the bottom of the graph, the DFS does not allow me to revisit that those nodes. Hence, any updates I make once I discover the end of the path can not be processed.
My approach may be totally off, but I am wondering if someone could help improve my implementation design, or explain how the implementation is commonly done. Thanks in advance.
You don't need that availability check to do a topological sort with DFS.
DFS itself ensures that you don't leave a node until its children have already been processed, so if you add each node to a list when DFS finishes with it, they will be added in (reverse) topological order.
Don't forget to do the whole graph, though, like this:
def toposort(graph):
visited = [False for i in range(len(graph))]
result = []
def DFS(node):
if visited[node]:
return
visited[node] = True
for adj in graph[node]:
DFS(adj)
result.append(node)
for i in range(len(graph)):
DFS(i)
return result
class Graph:
def __init__(self):
self.edges = {}
def addNode(self, node):
self.edges[node] = []
def addEdge(self, node1, node2):
self.edges[node1] += [node2]
def getSub(self, node):
return self.edges[node]
def DFSrecu(self, start, path):
for node in self.getSub(start):
if node not in path:
path = self.DFSrecu(node, path)
if start not in path:
path += [start]
return path
def topological_sort(self, start):
topo_ordering_list = self.DFSrecu(start, [])
# this for loop it will help you to visit all nodes in the graph if you chose arbitrary node
# because you need to check if all nodes in the graph is visited and sort them
for node in g.edges:
if node not in topo_ordering_list:
topo_ordering_list = g.DFSrecu(node, topo_ordering_list)
return topo_ordering_list
if __name__ == "__main__":
g = Graph()
for node in ['S', 'B', 'A', 'C', 'G', 'I', "L", 'D', 'H']:
g.addNode(node)
g.addEdge("S", "A")
g.addEdge("S", "B")
g.addEdge("B", "D")
g.addEdge("D", "H")
g.addEdge("D", "G")
g.addEdge("H", "I")
g.addEdge("I", "L")
g.addEdge("G", "I")
last_path1 = g.topological_sort("D")
last_path2 = g.topological_sort("S")
print("Start From D: ",last_path1)
print("start From S: ",last_path2)
Output:
Start From D: ['L', 'I', 'H', 'G', 'D', 'A', 'B', 'S', 'C']
start From S: ['A', 'L', 'I', 'H', 'G', 'D', 'B', 'S', 'C']
you can see here 'C' is included in topological sorted list even it's not connect to any other node but 'C' in the graph and you need to visited her
that's way you need for loop in topological_sort() function

making the loop to continue to produce for outputs

Here is the code I have:
def generate(x)
two = {}
for x in range(1,7315):
two.update({vowels[random.randint(0,4)] + alpha[random.randint(0,21)]:0})
return two
generate(x)
this only returns a single value, how could I make it return multiple values?
return a tuple with your values
def returnTwoNumbers():
return (1, 0)
print(returnTwoNumbers()[0])
print(returnTwoNumbers()[1])
#output:
#1
#0
It also looks like you're trying to get a random vowel from your list of vowels. Using random.choice is a much more readable way to get a random item from a list:
import random
vowelList = ['a', 'e', 'i', 'o', 'u']
print (random.choice(vowelList))
You can use a tuple to return multiple values from a function e.g.:
return (one, two, three)
You have wrong indentation
def generate():
two = {}
for x in range(1,7315):
two.update({vowels[random.randint(0,4)] + alpha[random.randint(0,21)]:0})
return two
twos = generate()

Saving output of a function to a text file

I have a function that takes the items in multiple lists and permutates them. So if I have the lists child0 = ['a', 'b'] and child1 = ['c', 'd']:
def permutate():
for i in child0:
for k in child1:
print (i, k)
permutate()
# a c
# a d
# b c
# b d
I'm running into problems with saving the output into a text file. I can't assign a var to the print statement because the output will change every time it runs through obviously, and writing the permutate() function to a text file does nothing. Doing a return instead of print won't run the permutation properly.... any tips on how to print all the permutations to a text file properly?
You need to build a list and return that list object:
def permutate():
result = []
for i in child0:
for k in child1:
result.append((i, k))
return result
for pair in permutate():
print(*pair)
What you are doing is creating the cartesian product, not the permutations.
The Python standard library has a function to do just this already, in itertools.product():
from itertools import product
list(product(child0, child1))
would produce the exact same list:
>>> from itertools import product
>>> child0 = ['a', 'b']
>>> child1 = ['c', 'd']
>>> for pair in product(child0, child1):
... print(*pair)
...
a c
a d
b c
b d
Pass a file object as an argument, and use file argument of print function.
def permutate(f):
for i in child0:
for k in child1:
print(i, k, file=f)
with open('testfile.txt', 'w') as f:
permutate(f)

Categories

Resources