How to divide python list into sublists of unequal

2020-05-05 17:05发布

I am trying to divide a list of elements which are comma separated into chunks of unequal length. How can I divide it?

list1 = [1, 2, 1]
list2 = ["1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4"]

list1 contains the elements which are the sizes of chunks which I wish to divide the list2 in.

标签: python list
5条回答
ゆ 、 Hurt°
2楼-- · 2020-05-05 17:26

Something like:

def divideUnequal(list1, list2):
    counter=0
    step=0
    divided=[]
    for count in list1:
            step= counter+ count
            sublist= list2[counter: step]
            counter= step
            divided.append(sublist)
    return divided
查看更多
够拽才男人
3楼-- · 2020-05-05 17:41

You can also use the .pop() method like this:

list1 = [1, 2, 1]
list2 = ["1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4"]

new_list = []
for chunk in list1:
    new_list.append( [ list2.pop(0) for _ in range(chunk)] )
print(new_list)


# [['1.1.1.1'], ['1.1.1.2', '1.1.1.3'], ['1.1.1.4']]

This will modify the original list2.

查看更多
小情绪 Triste *
4楼-- · 2020-05-05 17:42

You could combine the power of itertools.accumulate and list comprehensions:

In [4]: from itertools import accumulate

In [5]: data = ["1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4"]

In [6]: lengths = [1, 2, 1]

In [7]: [data[end - length:end] for length, end in zip(lengths, accumulate(lengths))]
Out[7]: [['1.1.1.1'], ['1.1.1.2', '1.1.1.3'], ['1.1.1.4']]

itertools.accumulate returns an iterator to a sequence of accumulated sums. This way you could easily calculate the end of each chunk in the source array:

In [8]: list(accumulate(lengths))
Out[8]: [1, 3, 4]
查看更多
小情绪 Triste *
5楼-- · 2020-05-05 17:43

You can use itertools.islice for this too. It's efficient and easy to read:

def unequal_divide(iterable, chunks):
    it = iter(iterable)
    return [list(islice(it, c)) for c in chunks]

Then to use it:

>>> list1 = [1, 2, 1]
>>> list2 = ["1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4"]
>>> unequal_divide(list1, list2)
[['1.1.1.1'], ['1.1.1.2', '1.1.1.3'], ['1.1.1.4']]

Or as a generator:

def unequal_divide(iterable, chunks):
    it = iter(iterable)
    for c in chunks:
        yield list(islice(it, c))

In use:

>>> list(unequal_divide(list1, list2))
[['1.1.1.1'], ['1.1.1.2', '1.1.1.3'], ['1.1.1.4']]

This is also implemented in more-itertools.split_at. See here for their source code which is almost identical minus allowing no chunks to be provided which is weird.

查看更多
Luminary・发光体
6楼-- · 2020-05-05 17:46

Yet another solution

list1 = [1,2,1]
list2 = ["1.1.1.1","1.1.1.2","1.1.1.3","1.1.1.4"]

chunks = []
count = 0
for size in list1:
    chunks.append([list2[i+count] for i in range(size)])
    count += size
print(chunks)

# [['1.1.1.1'], ['1.1.1.2', '1.1.1.3'], ['1.1.1.4']]
查看更多
登录 后发表回答