python - 如何遍历列表以创建 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]
- 首先,您将拥有一个基本映像:
image_base = Image.open (image_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)
list = [a, b, c, d, e, f]
- 目标是获得三个新图像;由于模块内的粘贴功能,将ab、cd和ef组合放置在基本图像上方。
解决方案
如果我正确理解您的问题,您只是想找到一种方法将列表中的 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() 方法将它们放回列表中,但您可以将其替换为任务中所需的任何函数。
推荐阅读
- amazon-web-services - 阻止混合内容 EC2 HTTPS
- python - 如何单击没有 ID 的元素
- postgresql - 将数组字段存储在新表列中
- php - ziparchive 文件夹和文件排除当前文件夹
- r - R函数没有赋值
- java - 如何使用 spring-security-saml2 配置服务提供者以使用 EncryptedAssertions?
- javascript - 改进管脚输入元件
- python - 如何在 Git Bash 上更新 python?
- r - 以最有效的方式确保 dplyr::summarise() 中的唯一值
- javascript - 如何为有间隔的任务使用循环?