首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >For循环重复并生成比指定值更多的值

For循环重复并生成比指定值更多的值
EN

Stack Overflow用户
提问于 2021-01-28 12:34:57
回答 2查看 42关注 0票数 0

我正试着在画布上粘贴3个矩形图像。目标是图像在画布内,并且它们不会重叠。为此,我决定生成3个二元组的值,它们将用作图像左上角的xy坐标的位置。如下所示:

代码语言:javascript
运行
复制
locations_used = [(950, 916), (1097, 119), (1290, 526)]

相反,它所做的是重复第一个值3x,然后添加两个新值,当我指定3时,总共得到5个位置。如下所示:

代码语言:javascript
运行
复制
[(950, 916), (950, 916), (950, 916), (1097, 119), (1290, 526)]

这是我的代码的MRE:

代码语言:javascript
运行
复制
    n = 3
    canvas_width = 500
    canvas_height = 300
    logo_width = 50
    logo_height = 30
    locations_used = []
    
    for i in range(0, n):
        
        logo_location_x, logo_location_y = logo_position(canvas_width, canvas_height, logo_width, logo_height)
        locations_used.append((logo_location_x, logo_location_y))

        for img in locations_used:
            if logo_location_x in range(img[0], logo_width) or logo_location_y in range(img[1], logo_height):
                pass
        else:
            while len(locations_used) < n:
                locations_used.append((logo_location_x, logo_location_y))

     print(len(locations_used))
     print(locations_used)
     print('\n')

输出:

代码语言:javascript
运行
复制
5
[(950, 916), (950, 916), (950, 916), (1097, 119), (1290, 526)]
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-01-28 12:49:51

在第一次迭代中,将logo_location_x(950)和logo_location_y(916)与range(950, 50)range(916, 30)进行比较。由于start参数比stop小,所以range为空,程序继续执行else子句。当locations_used的长度小于3时,将添加相同的值,从而使数组成为[(950, 916), (950, 916), (950, 916]

在接下来的两次迭代中,每个新的x, y对都会添加到locations_used中。当range(img[0], logo_width)range(img[1], logo_height)仍然为空时,locations_used的长度大于n,因此不会添加额外的元素。

这是经过编辑的代码,用于创建n而不是重叠位置。

代码语言:javascript
运行
复制
# Instead of iterating only 3 times, try until list is full.
while len(locations_used) < n:
        
    logo_location_x, logo_location_y = logo_position(canvas_width, canvas_height, logo_width, logo_height)
    
    # Don't add position until it is checked that it doesn't overlap.
    # locations_used.append((logo_location_x, logo_location_y))

    # Check if new rectangle candidate overlaps with previous ones.
    overlap = False
    for img in locations_used:
        # Fix range usage. I hope this is what you want.
        if logo_location_x in range(img[0] - logo_width, img[0] + logo_width) and logo_location_y in range(img[1] - logo_height, img[1] + logo_height):
            overlap = True
            break
    
    # Add position to list after you ensure that new position
    if not overlap:
        locations_used.append((logo_location_x, logo_location_y))
票数 1
EN

Stack Overflow用户

发布于 2021-01-28 12:49:59

您的else语句没有正确缩进。尝尝这个

代码语言:javascript
运行
复制
if logo_location_x in range(img[0], logo_width) or logo_location_y in range(img[1], logo_height):
    pass
else:
    while len(locations_used) < n:
            locations_used.append((logo_location_x, logo_location_y))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65931082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档