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 parse and group hierarchical list items from an unindented string in Python? ann23fr 0 91 Mar-27-2024, 01:16 PM
Last Post: ann23fr
  Help with to check an Input list data with a data read from an external source sacharyya 3 318 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  difference between forms of input a list to function akbarza 6 928 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 991 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 1,091 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 1,033 Jun-12-2023, 05:40 PM
Last Post: snippsat
  How do you get Python to print just one value in a list? 357mag 3 967 May-17-2023, 09:52 PM
Last Post: rob101
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  restrict user input to numerical values MCL169 2 869 Apr-08-2023, 05:40 PM
Last Post: MCL169
  list the files using query in python arjunaram 0 649 Mar-28-2023, 02:39 PM
Last Post: arjunaram

Forum Jump:

User Panel Messages

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