Home > database >  Using list of queues in a list
Using list of queues in a list

Time:12-06

    num_of_stacks = int((len(config[0])   1)/4)
    stacks = [Queue() for i in range(num_of_stacks)]
    # stacks = list(map(Queue, range(num_of_stacks)))
    print(stacks)
    for line in config[len(config)-2::-1]:
        print(stacks)
        for i in range(0, len(line), 4):
            print(int(i/4), line[i 1: i 2])
            if line[i 1: i 2] != ' ':
                print(stacks[int(i/4)])
                stacks[int(i/4)].put(line[i 1: i 2])
        print(line)

I am writing a program to solve newest Advent of code challange.

Tried to create a list of queues like this:

stacks = list(map(Queue, range(num_of_stacks)))

While iterating over it my whole program freezes. On the other hand, creating list of queues using list comprehension resolves the issue:

stacks = [Queue() for i in range(num_of_stacks)]

Input example:

                [M]     [W] [M]    
            [L] [Q] [S] [C] [R]    
            [Q] [F] [F] [T] [N] [S]
    [N]     [V] [V] [H] [L] [J] [D]
    [D] [D] [W] [P] [G] [R] [D] [F]
[T] [T] [M] [G] [G] [Q] [N] [W] [L]
[Z] [H] [F] [J] [D] [Z] [S] [H] [Q]
[B] [V] [B] [T] [W] [V] [Z] [Z] [M]
 1   2   3   4   5   6   7   8   9 

Does anyone can explain why two codes, that while debugging returns same objects, works differently in case of putting data inside queue?

CodePudding user response:

These two expressions aren't equivalent:

stacks = list(map(Queue, range(num_of_stacks)))
stacks = [Queue() for i in range(num_of_stacks)]

because map calls the function with the elements of the iterable as an argument. So the equivalent list comprehension to your map call would be:

stacks = [Queue(i) for i in range(num_of_stacks)]

You didn't provide the code for your Queue class, but I'm inferring that it does something bad when you pass an argument to its constructor.

  • Related