将一个列表按指定长度分割成多个列表

Posted zxpo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将一个列表按指定长度分割成多个列表相关的知识,希望对你有一定的参考价值。


def list_of_groups(init_list, childern_list_len):
    ‘‘‘
    :param init_list:
    :param childern_list_len:
    :return:
    ‘‘‘
    list_of_group = zip(*(iter(init_list),) *childern_list_len) # zip(childern_list_len ge list_iterator object) 
    end_list = [list(i) for i in list_of_group] # i is a tuple
    count = len(init_list) % childern_list_len
    end_list.append(init_list[-count:]) if count !=0 else end_list
    return end_list

以上是关于将一个列表按指定长度分割成多个列表的主要内容,如果未能解决你的问题,请参考以下文章