简体   繁体   English

如何获得循环遍历列表的方式?

[英]How can I get my loop to iterate through my list?

I'm new to programming and python. 我是编程和python的新手。 I've built a loop that creates a tiled image using one image, and it works great. 我建立了一个循环,使用一个图像创建平铺图像,效果很好。

for left in range(0,iWidth,(logoWidth + xOffset)):
    for top in range (0,iHeight,(logoHeight + yOffset)):
        icopyIm2.paste(logo,(left,top))

icopyIm2.save("tiled_image.png")

However, I'd like it to use a series of images, such that each tile is different. 但是,我希望它使用一系列图像,以便每个图块都不同。 In the parameters for "paste", the "logo" variable is only one image. 在“粘贴”的参数中,“徽标”变量仅是一张图像。 I'd like the loop to iterate through a list, for example: 我希望循环遍历列表,例如:

imageList = [pic1.png, pic2.png, pic3.png, pic4.png] imageList = [pic1.png,pic2.png,pic3.png,pic4.png]

I'm not sure how to achieve this. 我不确定如何实现这一目标。

As stated in comments, you need to figure out how you want to handle the different images when you tile them. 如评论中所述,您需要弄清楚平铺不同图像时的处理方式。 What do you want it to look like if your frame is 3x3 and you have 5 images? 如果您的帧是3x3并且有5张图像,您希望它看起来像什么?

I think the heart of your question is setting up a function with parameters. 我认为您的问题的核心是设置带有参数的函数。 The below example makes a tiled images of 3-letter words, which I think is inline with your question. 以下示例制作了一个由3个字母组成的单词的平铺图像,我认为这与您的问题一致。 I have chosen to just loop through the list of inputs repeatedly--no pattern. 我选择只是反复遍历输入列表-无模式。

def make_box(width, height, hits):
    hit_limit = len(hits)
    count = 0
    for i in range(height):
        for j in range(width):
            print(hits[count % hit_limit], end=' ')
            count += 1
        print()

hit_list = ['bop', 'pow', 'zap', 'bam', 'oof']
box_h = 3
box_w = 4

make_box(box_w, box_h, hit_list)

Output: 输出:

bop pow zap bam 
oof bop pow zap 
bam oof bop pow 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让我的代码遍历列表? - How can I get my code to iterate through the list? 如何获取嵌套循环函数以遍历整个(至少2个)列表变量并将其追加到新的列表输出中? - How do I get my nested loop function to iterate through the entire (at least 2) list variables and append to a new list output? 每次遍历列表时,如何获取要更改的索引号? - How to get the index number I'm searching over to change every time I iterate through my list? 如何在 jinja2 flask 应用程序中遍历我的列表? - How can i iterate through my list in jinja2 flask app? 每次我的外部 for 循环迭代时,我如何让我的内部 for 循环迭代? - How do I get my inner for loop to iterate every time my outer for loop iterates? 我无法遍历我的列表,它只是说“列表索引必须是整数或切片,而不是 str” - I can't iterate through my list, it just says "list indices must be integers or slices, not str" 为什么我不能循环遍历`requests`中的`payload`来迭代我的web scrape? - Why can't I loop through a `payload` in `requests` to iterate my web scrape? 如何在 Python 中遍历此列表 - How can I iterate through this list in Python 如何遍历列表? - How can I iterate through a list? 如何遍历列表的索引? - How can I iterate through the indices of the list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM