1

I'm struggling to update the values of my lists using a nested loop. My goal is to update the output lists in two waves.

The first wave: all even index lists within the order_set should be assigned to the output lists.

The second wave: all odd index lists within the order_set should be assigned to the output lists.

You'll see below that end output values are incorrect and the second wave never switches to the odd indices.

I'd appreciate any help here, I've been staring at these nested loops for too long--thank you!

End Goal Example:

# first wave:
output_1 = [1, 2, 3, 4]
output_2 = [2, 3, 4, 5]
output_3 = [3, 4, 5, 1]
output_4 = [4, 5, 1, 2]
output_5 = [5, 1, 2, 3]

# second wave:
output_1 = [1, 1, 1, 1]
output_2 = [1, 1, 1, 1]
output_3 = [1, 1, 1, 1]
output_4 = [1, 1, 1, 1]
output_5 = [1, 1, 1, 1]

# Instead, I'm seeing the following on both waves:
[4, 1, 2, 1]
[0, 5, 0, 3]
[3, 0, 1, 0]
[0, 4, 0, 2]
[5, 0, 5, 0]

The following are my variables and example code:

# First set of lists:
order_1 = [1, 2, 3, 4]
order_2 = [1, 1, 1, 1]
order_3 = [2, 3, 4, 5]
order_4 = [1, 1, 1, 1]
order_5 = [3, 4, 5, 1]
order_6 = [1, 1, 1, 1]
order_7 = [4, 5, 1, 2]
order_8 = [1, 1, 1, 1]
order_9 = [5, 1, 2, 3]
order_10 = [1, 1, 1, 1]

# Second set of lists:

output_1 = [0, 0, 0, 0]
output_2 = [0, 0, 0, 0]
output_3 = [0, 0, 0, 0]
output_4 = [0, 0, 0, 0]
output_5 = [0, 0, 0, 0]

# List of lists:
order_set = [order_1, order_2, order_3, order_4, order_5, order_6, order_7, order_8, order_9, order_10]

output_set = [output_1, output_2, output_3, output_4, output_5]

even_or_odd = 0

def assign_order(even_or_odd):

    index = 0
    if even_or_odd == 0:

        for j in range(0, len(order_set), 2):
            for i in range(len(output_set)-1):
                if index < 5:
                    output_set[index][i] = order_set[j][i]
                    index += 1
                else:
                    index = 0
                    output_set[index][i] = order_set[j][i]

        even_or_odd = 1

    elif even_or_odd == 1:
        for j in range(1, len(order_set), 2):
            for i in range(len(output_set)-1):
                if index < 5:
                    output_set[index][i] = order_set[j][i]
                    index += 1
                else:
                    index = 0
                    output_set[index][i] = order_set[j][i]

        even_or_odd = 1


print("Original Output:")
for i in range(len(output_set)):
    print(output_set[i])


print("Order Set")
for i in range(len(order_set)):
    print(order_set[i])

assign_order(even_or_odd)

print("Even Output:")
for i in range(len(output_set)):
    print(output_set[i])

assign_order(even_or_odd)

print("Odd Output:")
for i in range(len(output_set)):
    print(output_set[i])
Ciwonie
  • 45
  • 6
  • 1
    even_or_odd is always 0 and never changed in the caller's scope. While global variables are bad, if you decide to use it anyway, then you'll want to look up how to do so. https://stackoverflow.com/questions/423379/using-global-variables-in-a-function/423596#423596 – Kenny Ostrom Jul 01 '20 at 20:28
  • 1
    Do you understand hint from @KennyOstrom ? The assignment even_or_odd = 1 in assign_order() doesn't do anything useful because you have made it a local variable which dies (goes out of scope) when your function exits. As you code stands, it is simply a coincidence that there is a global with the same name. Look up variable scope and globals in python docs. – Tim Richardson Jul 01 '20 at 20:34
  • @TimRichardson Yes, thank you two for clarifying and providing further context. – Ciwonie Jul 02 '20 at 12:28

3 Answers3

3

You're doing way more work than you have to:

lists = [
    [1, 2, 3, 4],
    [1, 1, 1, 1],
    [2, 3, 4, 5],
    [1, 1, 1, 1],
    [3, 4, 5, 1],
    [1, 1, 1, 1],
    [4, 5, 1, 2],
    [1, 1, 1, 1],
    [5, 1, 2, 3],
    [1, 1, 1, 1]
]

first_wave = lists[::2]
second_wave = lists[1::2]

print(first_wave)
print(second_wave)

Output:

[[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 1], [4, 5, 1, 2], [5, 1, 2, 3]]
[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
>>> 
Paul M.
  • 10,481
  • 2
  • 9
  • 15
1

Here is a piece of work that should work:

# First set of lists:
order_1 = [1, 2, 3, 4]
order_2 = [1, 1, 1, 1]
order_3 = [2, 3, 4, 5]
order_4 = [1, 1, 1, 1]
order_5 = [3, 4, 5, 1]
order_6 = [1, 1, 1, 1]
order_7 = [4, 5, 1, 2]
order_8 = [1, 1, 1, 1]
order_9 = [5, 1, 2, 3]
order_10 = [1, 1, 1, 1]

# List of lists:
order_set = [order_1, order_2, order_3, order_4, order_5, order_6, order_7, order_8, order_9, order_10]

# Output set:
output_set = []

# Print order set
print("Order Set")
for i in range(len(order_set)):
    print(order_set[i])

# Get even index lists
for i, order_list in enumerate(order_set):
    if i % 2 == 0:
        output_set.append(order_list)

# Print output set
print("Even Output:")
for output_list in output_set:
    print(output_list)

# Reset the output set
output_set = []

# Get odd index lists
for i, order_list in enumerate(order_set):
    if i % 2 != 0:
        output_set.append(order_list)

# Print output set
print("Odd Output:")
for output_list in output_set:
    print(output_list)

You've done a lot of complex code to make something very simple. Let me explain to you what does i % 2 means: basically it gives you the rest of the operation i/2 so for example let say i = 0 then i % 2 is going to be equal to 0, another example if i = 1 then it's going to be equal to 1 because 1 is not dividable by 2 (except if you use decimal number). Thanks to that you can know if an index is even or odd.

Lucino772
  • 101
  • 1
  • 5
1
first_wave = []
second_wave = []
for i in range(0,len(order_set)):
    if i%2 == 0:
        first_wave.append(order_set[i])
    else:
        second_wave.append(order_set[i])
        
print(first_wave)
print(second_wave)

Output:

[[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 1], [4, 5, 1, 2], [5, 1, 2, 3]]
[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]

I believe the above will also solve the problem

Blind Rabit
  • 105
  • 8