如何将python列表分成不等长的子列表?

时间:2016-03-18 04:29:24

标签: python list

我试图将逗号分隔的元素列表分成不等长的块。我该怎么分呢?

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

list1包含我希望将list2分成几个块大小的元素。

5 个答案:

答案 0 :(得分:2)

您可以结合itertools.accumulate和列表推导的力量:

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返回一个累加和序列的迭代器。这样,您可以轻松计算源数组中每个块的结尾:

In [8]: list(accumulate(lengths))
Out[8]: [1, 3, 4]

答案 1 :(得分:1)

您也可以使用itertools.islice。它高效且易于阅读:

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

然后使用它:

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

或作为生成器:

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

使用中:

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

这也在more-itertools.split_at中实现。参见here以获得源代码,该源代码几乎是相同的负号,不允许提供任何奇怪的块。

答案 2 :(得分:1)

您还可以像这样使用.pop()方法:

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

这将修改原始的列表2

答案 3 :(得分:0)

类似的东西:

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

答案 4 :(得分:0)

另一种解决方案

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