如何将列表分成大小相等的子列表?
How to divide list into sublists of equal size?
我有一个作物列表,我需要将列表分成 48 个项目的子列表,然后将它们绘制成马赛克,我一直在手动进行。我怎样才能自动完成?
这是我使用的代码:
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
import numpy as np
p1 = listf[:48]
p2 = listf[48:96]
p3 = listf[96:144]
p4 = listf[144:192]
p5 = listf[192:240]
p6 = listf[240:288]
p7 = listf[288:336]
p8 = listf[336:384]
p9 = listf[384:432]
p10 = listf[432:480]
p11 = listf[480:528]
p12 = listf[528:576]
p13 = listf[576:624]
p14 = listf[624:642]
final = [p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14]
for idx, part in enumerate(final):
nc = 8
#fig = plt.figure(figsize=(8, (len(part)/6) * 8), dpi=600)
fig = plt.figure(figsize=(9, 6), dpi=300)
grid = ImageGrid(fig, 111, # similar to subplot(111)
#nrows_ncols=(int((len(part))/2), nc), # creates 12x2 grid of axes
nrows_ncols=(6, nc),
axes_pad=0.2, # pad between axes in inch.
)
for ax, im in zip(grid, part):
# Iterating over the grid returns the Axes.
ax.tick_params(labelbottom= False,labeltop = False, labelleft = False, labelright = False)
ax.imshow(cv2.cvtColor(im, cv2.COLOR_BGR2RGB))
fig.suptitle('Predicted vs Real', fontsize=15 )
my_lists = list(zip(*[iter(my_big_list)]*48))
是一种常见的(ish)模式,可以在没有 numpy 或 pandas 的情况下执行此操作,我认为
更具可读性的版本
split_size = 48
my_lists = [my_big_list[i:i+split_size] for i in range(0,len(my_big_list),split_size)]
如果你需要每组有48个,并且确定总数能被48整除:
final = np.array_split(listf, len(listf)//48)
如果需要14组:
final = np.array_split(listf, 14)
如果您不确定它是否能被 48 整除:
listf = [1,3] * 40 * 10
len_list = len(listf)
if len_list%48:
x = 48*(len_list//48)
temp_list = listf[x:]
listf = listf[:x]
final = np.array_split(listf, len(listf)//48)
final.append(temp_list)
print([len(x) for x in final])
输出,除最后一组外的所有 48 个:
[48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 32]
我有一个作物列表,我需要将列表分成 48 个项目的子列表,然后将它们绘制成马赛克,我一直在手动进行。我怎样才能自动完成?
这是我使用的代码:
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
import numpy as np
p1 = listf[:48]
p2 = listf[48:96]
p3 = listf[96:144]
p4 = listf[144:192]
p5 = listf[192:240]
p6 = listf[240:288]
p7 = listf[288:336]
p8 = listf[336:384]
p9 = listf[384:432]
p10 = listf[432:480]
p11 = listf[480:528]
p12 = listf[528:576]
p13 = listf[576:624]
p14 = listf[624:642]
final = [p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14]
for idx, part in enumerate(final):
nc = 8
#fig = plt.figure(figsize=(8, (len(part)/6) * 8), dpi=600)
fig = plt.figure(figsize=(9, 6), dpi=300)
grid = ImageGrid(fig, 111, # similar to subplot(111)
#nrows_ncols=(int((len(part))/2), nc), # creates 12x2 grid of axes
nrows_ncols=(6, nc),
axes_pad=0.2, # pad between axes in inch.
)
for ax, im in zip(grid, part):
# Iterating over the grid returns the Axes.
ax.tick_params(labelbottom= False,labeltop = False, labelleft = False, labelright = False)
ax.imshow(cv2.cvtColor(im, cv2.COLOR_BGR2RGB))
fig.suptitle('Predicted vs Real', fontsize=15 )
my_lists = list(zip(*[iter(my_big_list)]*48))
是一种常见的(ish)模式,可以在没有 numpy 或 pandas 的情况下执行此操作,我认为
更具可读性的版本
split_size = 48
my_lists = [my_big_list[i:i+split_size] for i in range(0,len(my_big_list),split_size)]
如果你需要每组有48个,并且确定总数能被48整除:
final = np.array_split(listf, len(listf)//48)
如果需要14组:
final = np.array_split(listf, 14)
如果您不确定它是否能被 48 整除:
listf = [1,3] * 40 * 10
len_list = len(listf)
if len_list%48:
x = 48*(len_list//48)
temp_list = listf[x:]
listf = listf[:x]
final = np.array_split(listf, len(listf)//48)
final.append(temp_list)
print([len(x) for x in final])
输出,除最后一组外的所有 48 个:
[48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 32]