Home > Software engineering >  How can I get 3 lists and variably merge them (python)
How can I get 3 lists and variably merge them (python)

Time:04-01

I have 3 lists and I need to change their order according to what the user asks for, like:

a = [1,2,3,4,5,6]
b = [7,8,9,10,11,12]
c = [13,14,15,16,17,18]
Result = [1,2,7,8,13,14,3,4,9,10,...,18]

In this case the user give me the number 2, I take two numbers from each list and make a fourth list.

After I go through the three lists, I need to put at the beginning of each new process a letter, like:

Final_list = [a,1,2,7,8,13,14,b,3,4,9,10,15,16,....]

CodePudding user response:

You can use .extend() along with a for loop that slices off step elements at a time for each list:

step = int(input())

data = [
 [1,2,3,4,5,6],
 [7,8,9,10,11,12],
 [13,14,15,16,17,18]
]

result = []
for i in range(0, len(data[0]), step):
    for sublist in data:
        result.extend(sublist[i:i step])

print(result)

CodePudding user response:

from itertools import cycle, islice

a = [1, 2, 3, 4, 5, 6]
b = [7, 8, 9, 10, 11, 12]
c = [13, 14, 15, 16, 17, 18]

def round_robin(*lists, n=2):
    iters = cycle(map(iter, lists))
    while (chunk := list(islice(next(iters), n))):
        yield from chunk

print(list(round_robin(a, b, c)))

Output:

[1, 2, 7, 8, 13, 14, 3, 4, 9, 10, 15, 16, 5, 6, 11, 12, 17, 18]
>>> 

This implementation will stop yielding values as soon as one of the iterators is exhausted (once the shortest list iterator is exhausted.)

CodePudding user response:

Here is one way to do so:

data = [[1, 2, 3, 4, 5, 6],
        [7, 8, 9, 10, 11, 12],
        [13, 14, 15, 16, 17, 18]]

number = 2

result = []
for i in range(0, len(data[0]), number):
    result  = [num for sublist in data for num in sublist[i:i number]]
print(result)  # [1, 2, 7, 8, 13, 14, 3, 4, 9, 10, 15, 16, 5, 6, 11, 12, 17, 18]

it can be simplified to a single list comprehension:

result = [num for i in range(0, len(data[0]), number) for sublist in data for num in sublist[i:i number]]
print(result)

Explanation

  • We execute a loop that advances a certain number of steps each time, in this case 2 by 2.
  • For each sub-list, we add 2 numbers from this sub-list.

The process is repeated until the end of the list is reached.

We can thus change the number of steps and the number of lists without any problem.


To better understand the list comprehension used, here is the version with for loops:

result = []
for i in range(0, len(data[0]), number):
    for sublist in data:
        for num in sublist[i:i number]:
            result.append(num)
print(result)

CodePudding user response:

Another approach:

a = [1,2,3,4,5,6]
b = [7,8,9,10,11,12]
c = [13,14,15,16,17,18]

x = int(input('Enter Number:'))
# x = 3

mat = [a,b,c]
grouped = [[row[x*i:x*(i 1)] for i in range(len(row)//x)] for row in mat]
result = [k for col in zip(*grouped) for tup in col for k in tup]

For an idea of what's going on, it's helpful to see what grouped looks like. For k=2,3, it's

[[[1, 2], [3, 4], [5, 6]],
 [[7, 8], [9, 10], [11, 12]],
 [[13, 14], [15, 16], [17, 18]]]

and

[[[1, 2, 3], [4, 5, 6]],
 [[7, 8, 9], [10, 11, 12]],
 [[13, 14, 15], [16, 17, 18]]]

respectively.

CodePudding user response:

OK... I think I understand that you want to pick a given number off each list in turn. I'm not sure if the lists you give it are necessarily the same length, so perhaps a little generator that reacts sensibly to list exhaustion:

def pick_n_from_lists(lists, n):
    genset = [iter(a_list) for a_list in lists]
    data_left = True
    while data_left:
        data_left = False
        for g in genset:
            try:
                for _ in range(n):
                    yield next(g)
                data_left = True
            except StopIteration:
                continue

fred = [6,7,8,9,10,11,12,13,14]
jim = [1,2,3,4]
bob = [19,18,17,16,15,14]
print("output:", list(pick_n_from_lists((fred,jim,bob),3)))

output: [6, 7, 8, 1, 2, 3, 19, 18, 17, 9, 10, 11, 4, 16, 15, 14, 12, 13, 14]
  • Related