如何迭代列表以创建n个连续值的组合,并在Python中独立使用它们


from PIL import Image
imagen_base = Image.open(imagen_base)
a = Image.open(a)
b = Image.open(b)
c = Image.open(c)
d = Image.open(d)
e = Image.open(e)
f = Image.open(f)

lista=[a,b,c,d,e,f]
  1. 首先,您将有一个基本图像:
image_base = Image.open (image_base)
  1. 其次,您会有一个图像列表:
a = Image.open(a)
b = Image.open(b)
c = Image.open(c)
d = Image.open(d)
e = Image.open(e)
f = Image.open(f)

list = [a, b, c, d, e, f]
  1. 目标是获得三张新图像;由于模块内的粘贴功能,将abcdef组合放置在基本图像上方

如果我正确理解你的问题,你只是想找到一种方法来将列表中的N个连续项目分组吗?如果是这样,你可以试试:

combined = []
for idx, val in enumerate(_list):
if (_list[idx] == 0) | (idx % 2 == 0): 
combined.append((_list[idx], _list[idx + 1]))

哪个将返回:

combined = [(a, b), (c, d), (e, f)]

现在,在这个例子中,为了清晰起见,我使用.append((方法将它们放回列表中,但您可以用任务中需要的任何函数来替换它。

相关内容

最新更新