问题
my_list = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,...,50]
answer
listOne = [0,1,2,....,9
listTwo = [10,11,12,...,19]
listThree = [20,21,22,...,29]
listFour = [30,31,32,...,39]
listFive = [40,41,42,...,49]
listSix = [50,51,52,...,59]
answer
如果我们不知道要在列表中显示的号码如何拆分列表
答案 0 :(得分:0)
def SplitList(given_list, chunk_size):
return [given_list[offs:offs+chunk_size] for offs in range(0, len(given_list), chunk_size)]
使用此功能传递列表:
chunk_list = SplitList(my_list, 10)
for lst in chunk_list:
print(lst)
答案 1 :(得分:0)
您可以使用mlist [i:i + 10]将组中的每10个元素分割
#populate list
mlist = []
for i in range (51):
mlist.append(i)
print("##########INPUT##########")
print(mlist)
new = []
for i in range(0, len(mlist), 10):
new.append(mlist[i : i+10])
print("##########OUTPUT##########")
print("Total Group: "+str(len(new)))
for i in range(len(new)):
print(new[i])
The output will be like this
##########INPUT##########
[0, 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]
##########OUTPUT##########
Total Group: 6
[0, 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]