Python Forum
converting python list to a list by user input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
converting python list to a list by user input
#1
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)
Reply
#2
Your question is still unclear to me.
Maybe you could be more detailed.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to revert back to a previous line from user input Sharkenn64u 2 978 Dec-28-2024, 08:02 AM
Last Post: Pedroski55
  User input with while loops chizzy101010 2 5,620 Aug-25-2024, 06:00 PM
Last Post: chizzy101010
  Problem with List Comprehension in Python laurawoods 3 1,160 Aug-12-2024, 06:26 AM
Last Post: Pedroski55
  how to capitalize letter in list object Python iwonkawa 4 1,321 May-29-2024, 04:29 PM
Last Post: DeaD_EyE
  Strange behavior list of list mmhmjanssen 3 1,631 May-09-2024, 11:32 AM
Last Post: mmhmjanssen
  Help with to check an Input list data with a data read from an external source sacharyya 3 1,693 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  difference between forms of input a list to function akbarza 6 2,383 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 2,541 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 2,649 Sep-24-2023, 05:03 AM
Last Post: deanhystad
Question Need help for a python script to extract information from a list of files lephunghien 6 2,586 Jun-12-2023, 05:40 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