Python 如何对嵌套元组列表中的每个浮点进行四舍五入

Python 如何对嵌套元组列表中的每个浮点进行四舍五入,python,coordinates,rounding,nested-lists,Python,Coordinates,Rounding,Nested Lists,我有一个坐标列表,如下所示: [[(-88.99716274669669, 45.13003508233472), (-88.46889143213836, 45.12912220841379), (-88.47075415770517, 44.84090409706577), (-88.75033424251002, 44.84231949526811), (-88.75283245650954, 44.897062864942406), (-88.76794136

我有一个坐标列表,如下所示:

[[(-88.99716274669669, 45.13003508233472), 
  (-88.46889143213836, 45.12912220841379), 
  (-88.47075415770517, 44.84090409706577), 
  (-88.75033424251002, 44.84231949526811), 
  (-88.75283245650954, 44.897062864942406),
  (-88.76794136151051, 44.898020801741716),
  (-88.77994787408718, 44.93415662283567), 
  (-88.99624763048942, 44.93474749747682), 
  (-88.99716274669669, 45.13003508233472)]]
[[(-88.99716274669669, 45.13003508233472)], 
 [(-88.46889143213836, 45.12912220841379), 
  (-88.47075415770517, 44.84090409706577)], 
 [(-88.75033424251002, 44.84231949526811), 
  (-88.75283245650954, 44.897062864942406), 
  (-88.76794136151051, 44.898020801741716)], 
 [(-88.77994787408718, 44.93415662283567), 
  (-88.99624763048942, 44.93474749747682), 
  (-88.99716274669669, 45.13003508233472)]]
[[(-88.99716274669669, 45.13003508233472, 10), 
  (-88.46889143213836, 45.12912220841379, 8)]]
或者像这样:

[[(-88.99716274669669, 45.13003508233472), 
  (-88.46889143213836, 45.12912220841379), 
  (-88.47075415770517, 44.84090409706577), 
  (-88.75033424251002, 44.84231949526811), 
  (-88.75283245650954, 44.897062864942406),
  (-88.76794136151051, 44.898020801741716),
  (-88.77994787408718, 44.93415662283567), 
  (-88.99624763048942, 44.93474749747682), 
  (-88.99716274669669, 45.13003508233472)]]
[[(-88.99716274669669, 45.13003508233472)], 
 [(-88.46889143213836, 45.12912220841379), 
  (-88.47075415770517, 44.84090409706577)], 
 [(-88.75033424251002, 44.84231949526811), 
  (-88.75283245650954, 44.897062864942406), 
  (-88.76794136151051, 44.898020801741716)], 
 [(-88.77994787408718, 44.93415662283567), 
  (-88.99624763048942, 44.93474749747682), 
  (-88.99716274669669, 45.13003508233472)]]
[[(-88.99716274669669, 45.13003508233472, 10), 
  (-88.46889143213836, 45.12912220841379, 8)]]
或者像这样:

[[(-88.99716274669669, 45.13003508233472), 
  (-88.46889143213836, 45.12912220841379), 
  (-88.47075415770517, 44.84090409706577), 
  (-88.75033424251002, 44.84231949526811), 
  (-88.75283245650954, 44.897062864942406),
  (-88.76794136151051, 44.898020801741716),
  (-88.77994787408718, 44.93415662283567), 
  (-88.99624763048942, 44.93474749747682), 
  (-88.99716274669669, 45.13003508233472)]]
[[(-88.99716274669669, 45.13003508233472)], 
 [(-88.46889143213836, 45.12912220841379), 
  (-88.47075415770517, 44.84090409706577)], 
 [(-88.75033424251002, 44.84231949526811), 
  (-88.75283245650954, 44.897062864942406), 
  (-88.76794136151051, 44.898020801741716)], 
 [(-88.77994787408718, 44.93415662283567), 
  (-88.99624763048942, 44.93474749747682), 
  (-88.99716274669669, 45.13003508233472)]]
[[(-88.99716274669669, 45.13003508233472, 10), 
  (-88.46889143213836, 45.12912220841379, 8)]]
嵌套、列表和元组项的数量是可变的

目前,我正在这样做:

import json

json.loads(json.dumps(list), parse_float=lambda x:round(float(x), 5))

JSON似乎没有必要(它已经是一个列表),但它简单易读。还有别的办法解决这个问题吗?

这是一种需要递归的方法

def round_all(stuff):
    if isinstance(stuff, list):
        return [round_all(x) for x in stuff]
    if isinstance(stuff, tuple):
        return tuple(round_all(x) for x in stuff)
    return round(float(stuff), 5)
我不知道“最快”(写得最快?读得最快?运行时?)

def re_round(li, _prec=5):
     try:
         return round(li, _prec)
     except TypeError:
         return type(li)(re_round(x, _prec) for x in li)
演示:

(函数的旧生成器版本,供后代使用:)