将float的元组转换为int [英] Convert tuple of tuples of floats to ints

查看:655
本文介绍了将float的元组转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

转换 ((2.0,3.1),(7.0,4.2),(8.9,1.0),(-8.9,7)) 到 ((2,3),(7,4),(8,1),(-8,7))

Convert ((2.0,3.1),(7.0,4.2),(8.9,1.0),(-8.9,7)) to ((2,3),(7,4),(8,1),(-8,7))

它可以将元组转换为numpy数组,然后应用.astype(int),但是还有更直接的方法吗?同样,我的解决方案"似乎太特殊了.

It works to convert the tuple to a numpy array, and then apply .astype(int), but is there a more direct way? Also my 'solution' seems too special.

它可以使用numpy

It works to use numpy

import numpy
data = ((2.0,3.1),(7.0,4.2),(8.9,1.0),(-8.9,7))
data1 = numpy.array(data)
data2 = data1.astype(int)
data3 = tuple(tuple(row) for row in data2)
data3 # ((2, 3), (7, 4), (8, 1), (-8, 7))

(((2,3),(7,4),(8,1),(-8,7)) 符合预期和期望

((2, 3), (7, 4), (8, 1), (-8, 7)) as expected and desired

推荐答案

In [16]: t = ((2.0,3.1),(7.0,4.2),(8.9,1.0),(-8.9,7))                                                                                                                                                                                                                                                                         

In [17]: tuple(tuple(map(int, tup)) for tup in t)                                                                                                                                                                                                                                                                             
Out[17]: ((2, 3), (7, 4), (8, 1), (-8, 7))

这篇关于将float的元组转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆