0

I have a list of sublist and want to reverse some pairs of this list. I want to find a way to define the pairs that should be reversed based on my data. This is list of pairs:

split_list=[[[4, 1], [3, 0], [2, 3], [0, 3], [2, 1], [1, 4]],\
            [[3, 1], [2, 1], [1, 0], [1, 1]]]

It has two sublists. Then I have the data that I want o use them to find where I should reverse the pairs:

chunk_len=[[np.array([[1., 2.]]), np.array([[1., 2.], [3., 4.]]), np.array([[0., 0.]])],\
           [np.array([[1., 2.]]), np.array([[1., 2.]])]]

I want to find the pairs that should be reversed based on the lengths of sublists stored in chunk_len. Length of first sublist in chunk_len is 3. Based on this length, first sublist of split_list should be divided into 3 chunks. Then, I want to reverse the pairs that are in even chunks. For second sublist, it has 2 chunks and I want to reverse the pairs of second one. Finally I want to have it as:

[[[4, 1], [3, 0], [3, 2], [3, 0], [2, 1], [1, 4]],\
 [[3, 1], [2, 1], [0, 1], [1, 1]]]

I tried the following but It was not sucessfull at all:

cor_spl=[]
for i,j in zip (split_list, chunk_len):
    for m in i:
        cor_spl.append(m)
        if m in i[len(j):int (len(j)+len(i))]:
            cor_spl.append (m[::-1])

In advance, I do appreciate any help.

2
  • 1
    I'm having hard time understanding the logic behind chunk_len, say how to decode the information in there. Can you explain it more clearly? Commented Apr 23, 2021 at 8:20
  • Dear @iGian len of each sublist in chunk_len defines the number of existing chunks in the same sublist of split_list Then, I wan to reverse even chunks. Commented Apr 23, 2021 at 8:24

2 Answers 2

1

First extract the length in a new array:

chunk_len = [[np.array([[1., 2.]]), np.array([[1., 2.], [3., 4.]]), np.array([[0., 0.]])],\
             [np.array([[1., 2.]]), np.array([[1., 2.]])]]

c_n = [len(l) for l in chunk_len]

Then iterate over the zipped pair. Then the chance number is even, process the data to reverse the array. For each chunk there is len(sub_list) // c_size two-element arrays to reverse, so you should iterate over those:

for (sub_list, c_size) in zip(split_list, c_n):
    for i in range(c_size):
        if i % 2 != 0:
            for j in range(len(sub_list) // c_size):
                j = i * (len(sub_list) // c_size) + j
                sub_list[j] = sub_list[j][::-1]
Sign up to request clarification or add additional context in comments.

4 Comments

Dear @Louis Lac, can you please check the sub_list. It has only the last sublist.
This algorithm modify split_list in place, this means that the result is in split_list, not in sub_list.
Dear @Louis Lac, Is there a way to save a modified version? I mean saving what I have shown in my question.
Yes, make a copy of the original array: result = split_list.copy(). Then apply the algorithm on this copy.
1

As we can see that in both 1st level nested list of split_list list we have 4 unit step. So logic works as follows :

LOGIC :
1. We iterate over each nested list with step 4 unit starting 2 index.
2. For each step update index and consecutive index in reverse order using this command:

i[k+l] = i[k+l][::-1]

CODE :

import numpy as np

split_list=[
    [[4, 1], [3, 0], [2, 3], [0, 3], [2, 1], [1, 4]],
    [[3, 1], [2, 1], [1, 0], [1, 1]]
]

chunk_len=[
    [
        np.array([[1., 2.]]), 
        np.array([[1., 2.], [3., 4.]]), 
        np.array([[0., 0.]])
    ],
    [
        np.array([[1., 2.]]), 
        np.array([[1., 2.]])
    ]
]


for i,j in zip (split_list, chunk_len):
    len_ = len(i)
    step_ = len_//len(j)

    for k in range(step_, len_, 2*step_):
        for l in range(step_):
            i[k+l] = i[k+l][::-1]

print(split_list)

OUTPUT :

[[[4, 1], [3, 0], [3, 2], [3, 0], [2, 1], [1, 4]], [[3, 1], [2, 1], [0, 1], [1, 1]]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.