Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将嵌套列表中的浮动舍入到小数点后2位?_Python_Coordinates_Rounding_Nested Lists - Fatal编程技术网

Python 如何将嵌套列表中的浮动舍入到小数点后2位?

Python 如何将嵌套列表中的浮动舍入到小数点后2位?,python,coordinates,rounding,nested-lists,Python,Coordinates,Rounding,Nested Lists,我有几个多边形的坐标列表,如下所示: l_b = [[[57.5, 2.875], [83.75, 4.1875], [83.75, 18.70923913043478], [57.50000000000001, 18.70923913043478], [57.5, 2.875]], [[83.75, 18.70923913043478], [57.50000000000001, 18.70923913043478], [57.5, 34.0869565

我有几个多边形的坐标列表,如下所示:

l_b =  [[[57.5, 2.875],
   [83.75, 4.1875],
   [83.75, 18.70923913043478],
   [57.50000000000001, 18.70923913043478],
   [57.5, 2.875]],
  [[83.75, 18.70923913043478],
   [57.50000000000001, 18.70923913043478],
   [57.5, 34.08695652173913],
   [83.75, 34.54347826086956],
   [83.75, 18.70923913043478]],
  [[0.0, 0.0],
   [18.125, 0.90625],
   [18.125, 16.70108695652174],
   [-2.530467720685112, 16.70108695652174],
   [0.0, 0.0]],
  [[18.125, 16.70108695652174],
   [-2.530467720685112, 16.70108695652174],
   [-5.0, 33.0],
   [18.125, 33.40217391304348],
   [18.125, 16.70108695652174]]]
如何使列表中的所有数字四舍五入到小数点后2位,同时保持其格式


我试过
l_b=['%.2f'%elem for elem in l_b]
但它给了我
TypeError:必须是实数,而不是list
你有3层嵌套列表,所以你需要向下钻取

l_b = [[['%.2f' % y[0], '%.2f' % y[1]] for y in x] for x in l_b]

@盖伊的解决方案有一个副作用。它使列表变得平坦

这可能更合适

l_b = [[['%.2f' % z for z in y] for y in x] for x in l_b]
如果列表是任意嵌套的,则可以使用递归。如果您的列表是三级嵌套的,则使用@Guy's answer或@mrEvgenX's answer

In [51]: def recur(lst):
    ...:     if isinstance(lst,float):
    ...:         return '%.2f'%lst #use round(lst,2) if you want float instead of string.
    ...:     else:
    ...:         return [recur(i) for i in lst]

In [52]: recur(l_b)

输出

[[['57.50', '2.88'],
  ['83.75', '4.19'],
  ['83.75', '18.71'],
  ['57.50', '18.71'],
  ['57.50', '2.88']],
 [['83.75', '18.71'],
  ['57.50', '18.71'],
  ['57.50', '34.09'],
  ['83.75', '34.54'],
  ['83.75', '18.71']],
 [['0.00', '0.00'],
  ['18.12', '0.91'],
  ['18.12', '16.70'],
  ['-2.53', '16.70'],
  ['0.00', '0.00']],
 [['18.12', '16.70'],
  ['-2.53', '16.70'],
  ['-5.00', '33.00'],
  ['18.12', '33.40'],
  ['18.12', '16.70']]]

您可以使用带有列表理解功能的内置函数
round

l_b = [[[round(e, 2) for e in j] for j in i] for i in l_b]

尝试这种简单的方法:

l_b = np.array( [[[57.5, 2.875],
   [83.75, 4.1875],
   [83.75, 18.70923913043478],
   [57.50000000000001, 18.70923913043478],
   [57.5, 2.875]],
  [[83.75, 18.70923913043478],
   [57.50000000000001, 18.70923913043478],
   [57.5, 34.08695652173913],
   [83.75, 34.54347826086956],
   [83.75, 18.70923913043478]],
  [[0.0, 0.0],
   [18.125, 0.90625],
   [18.125, 16.70108695652174],
   [-2.530467720685112, 16.70108695652174],
   [0.0, 0.0]],
  [[18.125, 16.70108695652174],
   [-2.530467720685112, 16.70108695652174],
   [-5.0, 33.0],
   [18.125, 33.40217391304348],
   [18.125, 16.70108695652174]]])
数组的形状是
(4,5,2)

应用map将小数四舍五入至2位

a=list(map(lambda x :round(x,2),l_b.flatten() ))
转换回原始形状

np.array(a).reshape(4,5,2)

作者建议的一种更简单的方法

您可以将
map()
round()
一起使用
round()
可以将所需的位数作为第二个参数

l_b中多边形的
:
对于多边形中的点:
点[:]=贴图(λx:圆形(x,2),点)

太复杂且效率低下。你可以只写
l_b.round(2)
。@Georgy,实际上我不知道也可以用这种方法来做,我会编辑我的回答。另外,需要注意的是,这种NumPy方法在多边形顶点数不同的情况下不起作用。
l_b.round(2)