Python:更改列表的形状

Python:更改列表的形状,python,multidimensional-array,list-comprehension,Python,Multidimensional Array,List Comprehension,使用python,我想转换以下列表: list_3_5 = [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]] 到另一个形状--一个由6个列表组成的列表,其中前3个新列表具有来自列表_3_5中3个主列表的前3个值,然后最后3个新列表具有来自列表_3_5的剩余值: [[1,2,3],[6,7,8],[11,12,13],[4,5],[9,10],[14,15]] 谢谢你的帮助 更新: 对于较长的列表,请执行以下操作: list2=[list(范围(1,34)

使用python,我想转换以下列表:

list_3_5 = [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]]
到另一个形状--一个由6个列表组成的列表,其中前3个新列表具有来自列表_3_5中3个主列表的前3个值,然后最后3个新列表具有来自列表_3_5的剩余值:

[[1,2,3],[6,7,8],[11,12,13],[4,5],[9,10],[14,15]]
谢谢你的帮助

更新:

对于较长的列表,请执行以下操作:

list2=[list(范围(1,34)),list(范围(34,67)),list(范围(67100))]
list2b=[list2[i][:5]表示范围内的i(0,len(list2))]+[
列表2[i][5:]表示范围内的i(0,len(列表2))
]
还是这个

list2=[list(范围(1,34)),list(范围(34,67)),list(范围(67100))]
list2b=列表(范围(2*len(list2)))
对于键,枚举中的元素(列表2):
list2b[键]=元素[:5]
list2b[key+len(list2)]=元素[5:]
给出以下内容(将我原来的3个列表(每个列表包含33个元素)修改为6个列表,前三个列表各包含5个元素,后三个列表各包含28个元素):

但是我想要的是这样的(在这个例子中,一个19个列表的列表,每个列表有5个元素,最后一个列表有剩余的4个元素):

看起来像这样,但我不太明白如何修改我的案例:

试试这个:

[list_3_5[0][:3],list_3_5[1][:3], list_3_5[2][:3], list_3_5[0][3:],list_3_5[1][3:], list_3_5[2][3:]]
输出:

[[1, 2, 3], [6, 7, 8], [11, 12, 13], [4, 5], [9, 10], [14, 15]]
试试这个:

[list_3_5[0][:3],list_3_5[1][:3], list_3_5[2][:3], list_3_5[0][3:],list_3_5[1][3:], list_3_5[2][3:]]
输出:

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

下面是一个具有双for循环的解决方案,可用于任意长度的列表:

list_3_5 = [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]]
newlist = []

for element in list_3_5:
    newlist.append(element[:3])

for element in list_3_5:
    newlist.append(element[3:])    

newlist

Out: [[1, 2, 3], [6, 7, 8], [11, 12, 13], [4, 5], [9, 10], [14, 15]]
但是等等!还有更多:

list_3_5 = [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]]
newlist = list(range(2*len(list_3_5)))

for key, element in enumerate(list_3_5):
    newlist[key] = element[:3]
    newlist[key+len(list_3_5)] = element[3:]

newlist
[[1, 2, 3], [6, 7, 8], [11, 12, 13], [4, 5], [9, 10], [14, 15]]
以下部分回答了问题的更新部分:

biglist = []
for i in list2:
    biglist = biglist + i
#Here we put all small lists in the given list into a big list
print(biglist)

#construct an empty list to save the result as we go:
newlist = []

#add sections of 5 to newlist
while len(biglist) >= 5:
    newlist.append(biglist[:5])
    biglist = biglist[5:]

#add the remaining part of biglist to the newlist:
newlist.append(biglist)
print(newlist)
输出为:

[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25], [26, 27, 28, 29, 30], [31, 32, 33, 34, 35], [36, 37, 38, 39, 40], [41, 42, 43, 44, 45], [46, 47, 48, 49, 50], [51, 52, 53, 54, 55], [56, 57, 58, 59, 60], [61, 62, 63, 64, 65], [66, 67, 68, 69, 70], [71, 72, 73, 74, 75], [76, 77, 78, 79, 80], [81, 82, 83, 84, 85], [86, 87, 88, 89, 90], [91, 92, 93, 94, 95], [96, 97, 98, 99]]

下面是一个具有双for循环的解决方案,可用于任意长度的列表:

list_3_5 = [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]]
newlist = []

for element in list_3_5:
    newlist.append(element[:3])

for element in list_3_5:
    newlist.append(element[3:])    

newlist

Out: [[1, 2, 3], [6, 7, 8], [11, 12, 13], [4, 5], [9, 10], [14, 15]]
但是等等!还有更多:

list_3_5 = [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]]
newlist = list(range(2*len(list_3_5)))

for key, element in enumerate(list_3_5):
    newlist[key] = element[:3]
    newlist[key+len(list_3_5)] = element[3:]

newlist
[[1, 2, 3], [6, 7, 8], [11, 12, 13], [4, 5], [9, 10], [14, 15]]
以下部分回答了问题的更新部分:

biglist = []
for i in list2:
    biglist = biglist + i
#Here we put all small lists in the given list into a big list
print(biglist)

#construct an empty list to save the result as we go:
newlist = []

#add sections of 5 to newlist
while len(biglist) >= 5:
    newlist.append(biglist[:5])
    biglist = biglist[5:]

#add the remaining part of biglist to the newlist:
newlist.append(biglist)
print(newlist)
输出为:

[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25], [26, 27, 28, 29, 30], [31, 32, 33, 34, 35], [36, 37, 38, 39, 40], [41, 42, 43, 44, 45], [46, 47, 48, 49, 50], [51, 52, 53, 54, 55], [56, 57, 58, 59, 60], [61, 62, 63, 64, 65], [66, 67, 68, 69, 70], [71, 72, 73, 74, 75], [76, 77, 78, 79, 80], [81, 82, 83, 84, 85], [86, 87, 88, 89, 90], [91, 92, 93, 94, 95], [96, 97, 98, 99]]

如果要使用常规代码来分隔前三个元素,请尝试以下操作:

new_list=[list_3_5[i][:3] for i in range(0,len(list_3_5))]+[ list_3_5[i][3:] for i in range(0,len(list_3_5))]

如果要使用常规代码来分隔前三个元素,请尝试以下操作:

new_list=[list_3_5[i][:3] for i in range(0,len(list_3_5))]+[ list_3_5[i][3:] for i in range(0,len(list_3_5))]

正如我所问的,这个问题得到了完美的回答,谢谢!但我问得不好;我在最初的帖子中添加了这个问题。正如我所问的,这个问题得到了完美的回答,谢谢!但我问得不好;我在最初的帖子中添加了这个问题。正如我所问的,这个问题得到了完美的回答,谢谢!但我问得不好;我添加到我的初始帖子中。我对StackOverflow还是一个新手。。。。如果我像上面那样修改了我的问题,我真的应该重新发布一个全新的问题吗?@BrentGunderson无需提交新问题,修改就可以了。然而,更新部分的说明对我来说并不清楚。你能澄清一下语言吗?感谢您澄清刚才的问题更新了我的答案,包括更新部分的解决方案。希望这有帮助。这个问题完全回答了我的问题,谢谢!但我问得不好;我添加到我的初始帖子中。我对StackOverflow还是一个新手。。。。如果我像上面那样修改了我的问题,我真的应该重新发布一个全新的问题吗?@BrentGunderson无需提交新问题,修改就可以了。然而,更新部分的说明对我来说并不清楚。你能澄清一下语言吗?感谢您澄清刚才的问题更新了我的答案,包括更新部分的解决方案。希望这有帮助。