Python Forum
converting python list to a list by user input - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: converting python list to a list by user input (/thread-6272.html)



converting python list to a list by user input - Dante24 - Nov-13-2017

Hello. I have this algorithm for breadth search and depth search. AS you can see below, the variable parents holds my nodes. How can I change it so that it becomes an user input. Example: parents=4, but by user input it is: parents=input(). Same thing for the parents list in my code I need.

def DFS_dist_from_node(query_node, parents):
result = {}
stack =
stack.append((query_node, 0))
while len(stack) > 0:
print("stack=", stack)
node, dist = stack.pop()
result[node] = dist
if node in parents:
for parent in parents[node]:
stack_members = [x[0] for x in stack]
if parent not in stack_members:
stack.append((parent, dist + 1))
return result


def BFS_dist_from_node(query_node, parents):
result = {}
queue =
queue.append((query_node, 0))
while queue:
print("queue=", queue)
node, dist = queue.pop(0)
result[node] = dist
if node in parents:
for parent in parents[node]:
queue_members = [x[0] for x in queue]
if parent not in result and parent not in queue_members:
queue.append((parent, dist + 1))
return result


if __name__ == "__main__":
parents = dict()
parents = {'N1': ['N2', 'N3', 'N4'], 'N3': ['N6', 'N7'], 'N4': ['N3'], 'N5': ['N4', 'N8'], 'N6': ['N13'],
'N8': ['N9'], 'N9': ['N11'], 'N10': ['N7', 'N9'], 'N11': ['N14'], 'N12': ['N5']}
print("Depth-first search:")
dist = DFS_dist_from_node('N1', parents)
print(dist)

print("Breadth-first search:")
dist = BFS_dist_from_node('N1', parents)
print(dist)


RE: converting python list to a list by user input - heiner55 - Nov-21-2017

Your question is still unclear to me.
Maybe you could be more detailed.