Python Forum
list, map and put of the Queue in the Tree Data Structure
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list, map and put of the Queue in the Tree Data Structure
#1
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())
Reply
#2
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)
I speak Python but I don't speak English (I just read it a little). If I express myself badly, please blame the translator^^.
Reply
#3
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]
longmen likes this post
Reply
#4
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())
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
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.
Reply
#6
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 Nones.
longmen likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#7
Please disregard my last post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A data structure akulamartin 5 5,203 Nov-11-2016, 07:22 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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