将浮点数组转换为整数数组?
我有一个叫做 start
的数组。我想把它当作一个字典 G
的键来使用。
这里是这个数组的一个示例:
array([(1497315, 11965605), (1502535, 11967915), (1501785, 11968665),
(1520325, 11972295), (1522905, 11972805), (1504545, 11972865),
(1500465, 11973075), (1489695, 11975205), (1485855, 11978775),
(1499535, 11978955), (1508205, 11981745), (1521315, 11982615),
(1501215, 11983335), (1508595, 11985225), (1503045, 11986635),
(1522425, 11987745), (1512705, 11988255), (1519035, 11989185)...
这个 start
数组的长度是 50。
我想把 start
转换成整数类型,这样我就可以把它用作字典的键。
我试过用 type (start) is int
来确认它不是一个整数。这个 start
是由两列整数创建的,表示 x 和 y 坐标。
与之前的问题相关:
1 个回答
1
我觉得这和你之前的问题有关。
array = [(1497315.0, 11965605.0),(1502535.0, 11967915.0),(1501785.0, 11968665.0)]
print map(tuple, map(lambda x: map(int, x), array))
# [(1497315, 11965605), (1502535, 11967915), (1501785, 11968665)]
使用列表推导式。
print [[int(item) for item in items] for items in array]
如果你想把字典的键转换成int
类型,可以使用字典推导式。
d = {(15035.0, 119915.0): 'b', (15085.0, 119665.0): 'c', (14975.0, 11965.0): 'a'}
print {(int(k[0]), int(k[1])):v for k, v in d.iteritems()}
# {(15085, 119665): 'c', (14975, 11965): 'a', (15035, 119915): 'b'}