Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

python - For loop repeats and generates more values than what is specified

I am trying to paste 3 rectangular images on a canvas. The goal is that the images are within the canvas and they do not overlap. For this, I decided to generate 3 two-tuple values that will serve as the locations of the xy coordinates of the image's top-left corner. Something like this:

locations_used = [(950, 916), (1097, 119), (1290, 526)]

What it instead does is that it repeats the first value 3x and then adds the 2 new values, giving me a total of 5 positions when I've specified 3. Like this:

[(950, 916), (950, 916), (950, 916), (1097, 119), (1290, 526)]

This is an MRE of my code:


    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('
')

The output:

5
[(950, 916), (950, 916), (950, 916), (1097, 119), (1290, 526)]
question from:https://stackoverflow.com/questions/65931082/for-loop-repeats-and-generates-more-values-than-what-is-specified

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In the first iteration, logo_location_x(950) and logo_location_y(916) are compared to range(950, 50) and range(916, 30). Since start parameter is smaller than stop, range is empty and program procedes to else clause. While length of locations_used is under 3, same values will be added, thus making the array [(950, 916), (950, 916), (950, 916].

In the next two iterations, each new x, y pair get added to locations_used. While range(img[0], logo_width) and range(img[1], logo_height) are still empty, length of locations_used is greater than n so no additional element will be added.

This is edited code to create n not overlaping positions.

# 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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...