简体   繁体   English

Python将列表元素缩放到另一个列表中的某些元素

[英]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: 我在Python中产生了将近500,000种以下类型的列表质量:

    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. 我希望scaled_second_list继续。 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. 我试图找到一个矩形的2个不同的完美解剖。

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! 我希望这有帮助!

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

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