首页 > 解决方案 > 如何遍历列表以创建 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组合放置在基本图像上方。

标签: pythonpython-imaging-library

解决方案


如果我正确理解您的问题,您只是想找到一种方法将列表中的 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() 方法将它们放回列表中,但您可以将其替换为任务中所需的任何函数。


推荐阅读