在python中缩放元组中的每个元素

时间:2014-08-13 08:32:52

标签: python tuples operation

我有一个元组类型数据如下:

x =  (((-300, 49.3878), (-300, 400), (-220.045, 400), (-169.193, 204.22), (-300, 49.3878)))

我需要通过除以100来缩放元组中的每个元素。也就是说,输出结果应为:

x'= (((-3.00, 0.493878), (-3.00, 4.00), (-2.20045, 4.00), (-1.69193, 2.0422), (-3.00, 0.493878)))

有人可以告诉我该怎么做吗?

1 个答案:

答案 0 :(得分:3)

使用嵌套的list comprehension,如下所示。

x =  (((-300, 49.3878), (-300, 400), (-220.045, 400), (-169.193, 204.22), (-300, 49.3878)))

y = tuple(tuple(i / 100.0 for i in inner) for inner in x)

# You can remove the tuple and instead use [] if you don't mind it being a list.
y_list = [[i / 100.0 for i in inner] for inner in x]