简体   繁体   中英

Expand list with identical elements

Suppose I have the following nested list:

initial_list = [[1, 2, 3],
                [4, 5, 6],
                [7, 8, 9]]

I would like to turn it into the following:

desired_list = [[1, 1, 1, 2, 2, 2, 3, 3, 3], 
                [4, 4, 4, 5, 5, 5, 6, 6, 6], 
                [7, 7, 7, 8, 8, 8, 9, 9, 9]]

If I didn't care about order, I could do something like

new_list = [sorted(x*3) for x in initial_list]

However, the order should remain the same as in the initial_list . The best I could make is to put each element in a list and multiply it by 3 (arbitrary number), and then join the resulting inner_list :

multiplied_list = [[[element]*3 for element in inner_list] for inner_list in initial_list]
desired_list = [[element for element_list in inner_list for element in element_list] for inner_list in multiplied_list]

(In two lists for human comprehension)

Is there a more comprehensible/adequate/pythonic way to perform this?

You could just use the following list-comprehension. Note my initial_list is different to the one in the OP to demonstrate that order is preserved.

Code:

>>> initial_list = [[1, 3, 2], [4, 5, 6], [7, 8, 9]]
>>> [[x for x in sl for _ in range(3)] for sl in initial_list]
[[1, 1, 1, 3, 3, 3, 2, 2, 2],
 [4, 4, 4, 5, 5, 5, 6, 6, 6],
 [7, 7, 7, 8, 8, 8, 9, 9, 9]]

Or, add a key to the sorted function in your example:

>>> [sorted(x*3, key=x.index) for x in initial_list]
[[1, 1, 1, 3, 3, 3, 2, 2, 2],
 [4, 4, 4, 5, 5, 5, 6, 6, 6],
 [7, 7, 7, 8, 8, 8, 9, 9, 9]]

Time comparison of methods with varying n*n list size:

在此处输入图片说明

Generated using perfplot - code to reproduce:

from itertools import chain
from functools import reduce
import perfplot
from copy import deepcopy
import numpy as np
import random

def shuffle(x):
    random.shuffle(x)
    return x

def cdjb(initial_list):
    return [[x for x in sl for _ in range(3)] for sl in initial_list]

def aurora_sorted(initial_list):
    return [sorted(x*3, key=x.index) for x in initial_list]

def aurora_list_comp(initial_list):
    return [[element for element_list in inner_list for element in element_list] for inner_list in [[[element]*3 for element in inner_list] for inner_list in initial_list]]

def kederrac(initial_list):
    new_list = deepcopy(initial_list)
    for l in new_list:
        for j in range(0, 3*len(l), 3):
            l[j: j + 1] = [l[j]] * 3
    return new_list

def alain_chain(initial_list):
    return [list(chain(*(i3 for i3 in zip(*[sl]*3)))) for sl in initial_list]

def alain_reduce(initial_list):
    return [list(reduce(lambda a,e:a+[e]*3,sl,[]))for sl in initial_list]

def alain_zip(initial_list):
    return [[i for i3 in zip(*[sl]*3) for i in i3] for sl in initial_list]

def binyamin_numpy(initial_list):
    return np.array(initial_list).repeat(3).reshape(len(initial_list), -1).tolist()


perfplot.show(
    setup=lambda n: [shuffle([i for i in range(n)]) for j in range(n)],
    n_range=[2**k for k in range(12)],
    kernels=[
        cdjb,aurora_sorted, aurora_list_comp, kederrac, alain_chain, alain_reduce, alain_zip, binyamin_numpy
        ],
    xlabel='len(x)',
    )

numpy it, and 1 line of code:

 arr=np.array(initial_list)
 arr.repeat(3).reshape(3,-1)

output:

Out[44]: 
array([[1, 1, 1, 2, 2, 2, 3, 3, 3],
       [4, 4, 4, 5, 5, 5, 6, 6, 6],
       [7, 7, 7, 8, 8, 8, 9, 9, 9]])

here is a simple example using 2 for loops:

for l in initial_list:
    for j in range(0, 3*len(l), 3):
        l[j: j + 1] = [l[j]] * 3

I've been testing against @CDJB solution (with sorting):

from random import choice
def test1():
    initial_list = [[choice(range(1000)) for _ in range(1000)] for _ in range(100)] 
    def my_func(initial_list):
        for l in initial_list:
            for j in range(0, 3*len(l), 3):
                l[j: j + 1] = [l[j]] * 3
        return initial_list

    my_func(initial_list)

def test2():
    initial_list = [[choice(range(1000)) for _ in range(1000)] for _ in range(100)]  
    [sorted(x*3, key=x.index) for x in initial_list]

here are the results:

%timeit test2()
1.55 s ± 5.12 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

and:

%timeit test1()
165 ms ± 542 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

this simple solution is 9 times faster, depends on your data of course

You could do this using zip and chain (from itertools):

  from itertools import chain
  aList  = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 
  aList3 = [ list(chain(*(i3 for i3 in zip(*[sl]*3)))) for sl in aList ]

or using reduce from functools (which turned out to be much slower on larger lists):

  from functools import reduce
  aList  = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  aList3 = [ list(reduce(lambda a,e:a+[e]*3,sl,[]))for sl in aList ]

or zip with a nested comprehension (which is a bit faster than chain-zip):

  aList  = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  aList3 = [[i for i3 in zip(*[sl]*3) for i in i3] for sl in initial_list]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM