0

我有一个元组列表

b = [('676010', '5036043'), ('771968', '4754525'), ('772025', '4754525'), ('772072', '4754527'), ('772205', '4754539'), ('772276', '4754542'), ('772323', '4754541'), ('647206', '5036049')]

map()用来将它们转换为 type float

In [45]: print [map(float,e) for e in b]
[[676010.0, 5036043.0], [771968.0, 4754525.0], [772025.0, 4754525.0], [772072.0, 4754527.0], [772205.0, 4754539.0], [772276.0, 4754542.0], [772323.0, 4754541.0], [647206.0, 5036049.0]]

如何对带有 的元素进行数学运算map(),比如说除以 100,000?

我知道以不同的方式到达那里,但是我该如何实现map()呢?

In [46]: print [(float(tup[0])/100000,float(tup[1])/100000) for tup in b]
[(6.7601, 50.36043), (7.71968, 47.54525), (7.72025, 47.54525), (7.72072, 47.54527), (7.72205, 47.54539), (7.72276, 47.54542), (7.72323, 47.54541), (6.47206, 50.36049)]
4

2 回答 2

5

给 map 一个对每个元组进行操作的函数:

map(lambda t: (float(t[0]) / 100000, float(t[1]) / 100000), b)

甚至嵌套map()函数:

map(lambda t: map(lambda v: float(v) / 10000, t), b)

嵌套map()返回一个列表而不是一个元组。

就个人而言,我仍然会在这里使用列表理解:

[[float(v) / 10000 for v in t] for t in b]

演示:

>>> b = [('676010', '5036043'), ('771968', '4754525'), ('772025', '4754525'), ('772072', '4754527'), ('772205', '4754539'), ('772276', '4754542'), ('772323', '4754541'), ('647206', '5036049')]
>>> map(lambda t: (float(t[0]) / 100000, float(t[1]) / 100000), b)
[(6.7601, 50.36043), (7.71968, 47.54525), (7.72025, 47.54525), (7.72072, 47.54527), (7.72205, 47.54539), (7.72276, 47.54542), (7.72323, 47.54541), (6.47206, 50.36049)]
>>> map(lambda t: map(lambda v: float(v) / 10000, t), b)
[[67.601, 503.6043], [77.1968, 475.4525], [77.2025, 475.4525], [77.2072, 475.4527], [77.2205, 475.4539], [77.2276, 475.4542], [77.2323, 475.4541], [64.7206, 503.6049]]
>>> [[float(v) / 10000 for v in t] for t in b]
[[67.601, 503.6043], [77.1968, 475.4525], [77.2025, 475.4525], [77.2072, 475.4527], [77.2205, 475.4539], [77.2276, 475.4542], [77.2323, 475.4541], [64.7206, 503.6049]]
于 2014-01-30T14:26:41.743 回答
1
>>> map(lambda x: tuple([float(x[0])/100000, float(x[1])/100000]), b)
[(6.7601, 50.36043), (7.71968, 47.54525), (7.72025, 47.54525), (7.72072, 47.54527), (7.72205, 47.54539), (7.72276, 47.54542), (7.72323, 47.54541), (6.47206, 50.36049)]
>>>
于 2014-01-30T14:27:07.073 回答