从列表

时间:2016-03-11 17:18:11

标签: python list decimal

这应该很容易,但我很挣扎,我只是想从这个列表中的每个数字中删除小数位:

list = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]

我尝试的方法是:

int(list)round(list)但未作为only length-1 arrays can be converted to Python scalars

工作

有人可以提供建议吗?

3 个答案:

答案 0 :(得分:4)

使用list(map(int, list))[int(x) for x in list]不要使用list作为变量名称,因为它与内置类型冲突。此外,它不是很具描述性。您使用的名称取决于其用途,但不要使用覆盖内置类型的名称。

答案 1 :(得分:1)

尝试:

new_list = map(int, list)

“list”实际上也不是列表对象的好名字。

答案 2 :(得分:0)

使用此:

my_list = list(map(int,my_list))

map()函数将函数int()应用于序列my_list的所有元素 在python3 map()函数中将返回一个map对象,所以我们必须将它转换为一个列表。

相关问题