将字符串列表转换为字符串列表列表

时间:2014-04-04 04:19:28

标签: python

我有一个像这样的字符串列表:

['00000001000000000000000000000000', '11101000000000000000000000000000', '00011000000000000000000000000000', '11001000000000000000000000000000', '00101000000000000000000000000000', '10101000000000000000000000000000']

我想把它分成相同长度的块:

[['00000001000000000000000000000000', '11101000000000000000000000000000', '00011000000000000000000000000000'], ['11001000000000000000000000000000', '00101000000000000000000000000000', '10101000000000000000000000000000']]

任何人都可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

假设列表的长度完全可以被任意数N整除,这是一个基本脚本:

import copy
L = ['000','111','222','333','444','555','666','777','888']
new_L = []

chunk = []
for i, list_item in enumerate(L):
    chunk.append(list_item)
    if (i+1) % N == 0:
        new_L.append(copy.deepcopy(chunk))
        chunk=[]