简体   繁体   中英

Python Scaling list elements to certain elements in another list

I have nearly 500 000 of the following kind of list mass generated in Python:

    example_list = [height, width, [characteristics]]

With height, width, and every characteristic being a whole, positive number.

The first step in finding a solution to my problem is comparing the list height and width. I succeed in this step if they are equal or have the same ratio. I can compare the ratios but I can't seem to scale the characteristics to that ratio.

For example this could be a possible solution:

    first_list = [8, 12, [1, 2, 3, 4]]
    second_list = [4, 6, [5, 6, 7, 8]]

Because the second list scaled gives:

    scaled_second_list = [8, 12, [10, 12, 14, 16]]

The second list now has the same heigt and width as the first and the characteristics are also scaled, so I can continue further comparison. I would like the scaled_second_list to continue. But I can't seem to scale the characteristics.

Thanks in advance!

For those interested; I'm trying to find 2 different perfect dissections of a rectangle.

As you have already calculated the ratio, I think you are stuck at scaling up the characteristic sublist because its nested. One solution could be this, I believe this is not the most optimized and elegant way of doing it, but it will work.

first_list = [8, 12, [1, 2, 3, 4]]
second_list = [4, 6, [5, 6, 7, 8]]
scaled_second_list = []

ratio = 3

for i in second_list:
    characteristic = []
    if isinstance(i, list):
        for j in i:
            characteristic.append(j*2)
        scaled_second_list.append(characteristic)
    else:
        scaled_second_list.append(i*2)

print scaled_second_list

I hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM