Link_tester
Link_tester

Reputation: 1081

how to reverse some specific pairs of a list in python

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.

Upvotes: 0

Views: 681

Answers (2)

Davinder Singh
Davinder Singh

Reputation: 2162

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]]]

Upvotes: 1

Louis Lac
Louis Lac

Reputation: 6406

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]

Upvotes: 1

Related Questions