简体   繁体   English

在python的子列表中创建子列表

[英]create sublists within sublists in python

I'd like to create a series of sublists within a sublist: 我想在一个子列表中创建一系列子列表:

original = range(1,13)

I read another post that provided a solution to create sublists within a list: 我读了另一篇文章,提供了在列表中创建子列表的解决方案:

>>> [original[i:i+2] for i in range(0,len(original),2)]
>>> [[1,2], [3,4], [5,6], [7,8], [9,10], [11,12]]

But how can I achieve this result? 但是我怎样才能达到这个结果呢?

[ [[1,2],[3,4]], [[5,6],[7,8]], [[9,10],[11,12]] ] 

You can define your sublisting action as a function and apply it twice. 您可以将子列表操作定义为函数,然后将其应用两次。 This is probably inefficient since it will construct intermediate lists before constructing ones with the finest level of sublisting. 这可能效率不高,因为它将在构造具有最佳子列表级别的列表之前先构建中间列表。 But, is is easier to read since you're already familiar with the first step given that you used it when asking this question. 但是,由于您已经熟悉了第一步,因为您在问这个问题时已经使用了第一步,因此它更易于阅读。

def nest_in_pairs(original):
    return [original[i:i+2] for i in range(0,len(original),2)]

print nest_in_pairs(nest_in_pairs(original))

A more efficient way to do it would be to create a generator that yields a list of up to two items from the front of the list. 一种更有效的方法是创建一个生成器,该生成器从列表的最前面产生最多两个项目的列表。 Then chain them. 然后将它们链接。

from types import GeneratorType

def yield_next_two(seq):
    if not isinstance(seq, GeneratorType):
        for i in range(0, len(seq), 2):
            yield seq[i:i+2]
    else:
        while True:
            item1 = next(seq)
            try:
                item2 = next(seq)
                yield [item1, item2]
            except StopIteration:
                yield [item1]

pair_generator = yield_next_two(original)

quad_generator = yield_next_two(yield_next_two(original))

next(pair_generator)

next(quad_generator)

and you can call list on pair_generator or quad_generator to get the whole set of contents. 您可以致电listpair_generatorquad_generator得到一整套内容。

Here's an example of playing with this after pasting the above code into an IPython session: 这是将上面的代码粘贴到IPython会话后使用此示例的示例:

In [40]: quad_generator = yield_next_two(yield_next_two(original))

In [41]: list(quad_generator)
Out[41]: [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]]

In [42]: nearly_eights_generator = yield_next_two(yield_next_two(yield_next_two(original)))

In [43]: list(nearly_eights_generator)
Out[43]: [[[[1, 2], [3, 4]], [[5, 6], [7, 8]]], [[[9, 10], [11, 12]]]]
orig = range(1,13)
def squeeze( original ): 
    return [original[i:i+2] for i in range(0,len(original),2)]
print ( squeeze(squeeze(orig)) )

You could run the same code twice: 您可以运行相同的代码两次:

>>> original = range(1,13)
>>> [original[i:i+2] for i in range(0,len(original),2)]
output --> [[1,2], [3,4], [5,6], [7,8], [9,10], [11,12]]

>>> original = range(1,13)
>>> original = [original[i:i+2] for i in range(0,len(original),2)]
>>> [original[i:i+2] for i in range(0,len(original),2)]
output --> [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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