![]() |
list, map and put of the Queue in the Tree Data Structure - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: list, map and put of the Queue in the Tree Data Structure (/thread-36784.html) |
list, map and put of the Queue in the Tree Data Structure - longmen - Mar-29-2022 Hi, I wonder if anyone could explain me what does this line of code do " list(map(fringe.put, tree_struct))"? Thanks def createTree(leafValues): fringe = queue.Queue() list(map(fringe.put, tree_struct)) visited.add(root.str()) RE: list, map and put of the Queue in the Tree Data Structure - Coricoco_fr - Mar-30-2022 Hello, This for explain: class Example(): def __init__(self): pass def mymethod(self, name): return f"Hello {name}" ex = Example() out = list(map(ex.mymethod, ['me', 'you'])) print(out) RE: list, map and put of the Queue in the Tree Data Structure - ndc85430 - Mar-30-2022 map transforms the items in a iterable in some way. The iterable is passed as the second argument and a function that performs the transformation as the first. So, map calls the function on each of the items in the iterable producing a new one. It actually produces a generator, so list there turns that generator into a list.The class in the example above adds nothing, other than a bit of noise. >>> def double(x): ... return 2 * x ... >>> values = [1, 7, 4, 10, 9] >>> list(map(double, values)) [2, 14, 8, 20, 18]A list comprehension does the same: >>> [double(v) for v in values] [2, 14, 8, 20, 18] RE: list, map and put of the Queue in the Tree Data Structure - DeaD_EyE - Mar-30-2022 This is a fancy way calling fringe.put for each element of tree_struct .(Mar-29-2022, 10:38 PM)longmen Wrote: list(map(fringe.put, tree_struct)) This could be written as: for element in tree_struct: fringe.put(element)The try to explain this: from queue import Queue result_queue = Queue() values = (1, 2, 3, 4, 5, 6) print(list(map(result_queue.put, values))) # will do: # result_queue.put(1) # result_queue.put(2) # result_queue.put(3) # ... # # But the result is: # [None, None, None, None, None, None] # result_queue.put does return None # and not the element you've put into the queue print(f"Now {result_queue.qsize()} elements are in result_queue") # the elements are in the Queue print("\nElements in Queue:") # loop until result_queue is empty while not result_queue.empty(): print(result_queue.get()) RE: list, map and put of the Queue in the Tree Data Structure - jefsummers - Mar-30-2022 OK, but the first problem is that the list is created but then discarded. You need to assign the list to a variable, or print it, or do something with it as in the other posts above. Otherwise the only true function of that line is to throw an exception if the data does not fit. In other words, it is a data test but the result is not kept. RE: list, map and put of the Queue in the Tree Data Structure - DeaD_EyE - Mar-30-2022 Queue.put returns None , but the method is still called, and the elements are put into the queue.map can be used to make such funny things. The biggest disadvantage is that you can't do error handling for individual elements.The second disadvantage is, that the return value of map is not used. In this case, it's useless because there are only None s.
RE: list, map and put of the Queue in the Tree Data Structure - longmen - Mar-30-2022 Please disregard my last post |