Home > Blockchain >  How can I make my list print its elements in a fashioned order?
How can I make my list print its elements in a fashioned order?

Time:04-30

I currently have a populated list full of nodes in a path from one node to another. I need to stylize the printing of the path as such:

node -> node -> node -> node ->
node -> node

I currently have this code to do so:

        if len(path) %4 == 0:    
            for i in range(0, len(path)):
                if i 1 % 4 == 0:
                    print(print_queue[0] ' -> ' print_queue[1] ' -> ' print_queue[2] ' -> ' print_queue[3] ' -> ')
                    print_queue = []
                else:
                    print_queue.append(path[i])
        if len(path) %4 == 1:
            path.append(' ')
            for i in range(0, len(path)):
                if path[i] == ' ':
                    break
                if i 1 % 4 == 0:
                    print(print_queue[0] ' -> ' print_queue[1] ' -> ' print_queue[2] ' -> ' print_queue[3] ' -> ')
                    print_queue = []
                else:
                    print_queue.append(path[i])
        if len(path) %4 == 2:
            path.append(' ')
            for i in range(0, len(path)):
                if path[i] == ' ':
                    break
                if i 1 % 4 == 0:
                    print(print_queue[0] ' -> ' print_queue[1] ' -> ' print_queue[2] ' -> ' print_queue[3] ' -> ')
                    print_queue = []
                else:
                    print_queue.append(path[i])
        if len(path) %4 == 3:
            path.append(' ')
            for i in range(0, len(path)):
                if path[i] == ' ':
                    break
                if i 1 % 4 == 0:
                    print(print_queue[0] ' -> ' print_queue[1] ' -> ' print_queue[2] ' -> ' print_queue[3] ' -> ')
                    print_queue = []
                else:
                    print_queue.append(path[i])
        if print_queue != []:
            if len(print_queue) == 1:
                print(print_queue[0])
            if len(print_queue) == 2:
                print(print_queue[0] ' -> ' print_queue[1])
            if len(print_queue) == 3:
                print(print_queue[0] ' -> ' print_queue[1] ' -> ' print_queue[2])

But the path is not being printed. The path list is populated, but the program does not give any output.

What do I need to change in order for this path to print?

CodePudding user response:

It looks like you want to print each element in the path with " -> " between them.

print(" -> ".join(path))

should print them that way.

If you want a " -> " on the end too, just add one on the end in the print statement

print(" -> ".join(path)   " -> ")

so a path of ["a", "b", "c"] will print a -> b -> c ->

If you want it to break on multiples of 4 nodes you can break the array into chunks of 4 first

path = ["a", "b", "c", "d", "e"]
print(" -> \n".join([" -> ".join(path[i:i 4]) for i in range(0, len(path), 4)]))

which will print

a -> b -> c -> d -> 
e
  • Related