[英]how to convert a list of floats to int in python ;err int() argument must be a string, a bytes-like object or a number, not 'list'
I'm trying to convert my list of floats to ints, but I keep getting the error int() argument must be a string, a bytes-like object or a number, not 'list'
我正在尝试将浮点数列表转换为int,但我不断收到错误int() argument must be a string, a bytes-like object or a number, not 'list'
This is my code, and values is the list in floats. 这是我的代码,值是浮点数中的列表。
values = [my_random() for x in range(num_trials)]
vals_to = [int(x*100) for x in values]
and I'm doing this in jupyter notebook. 我正在使用jupyter笔记本进行此操作。 When I try samples they turn out fine, but I'm so confused on why this particular code of list is giving me this error. 当我尝试采样时,它们的结果很好,但是我对为什么列表的特定代码给我这个错误感到困惑。 Can someone please point me in the right direction? 有人可以指出正确的方向吗?
First, you need to make sure that your function my_random() returns a float number. 首先,您需要确保函数my_random()返回浮点数。
Anyway, let's say you have a list of float numbers 无论如何,假设您有一个浮点数列表
mylist = [3.1415, 74.2, 345.03, 98.9]
There are many ways to achieve your task. 有很多方法可以完成您的任务。 For example, 例如,
res = [int(i) for i in mylist] # 1 List comprehension
res = list(map(int, mylist)) # 2 Map
For more control on how to round each number, you can use math 若要控制每个数字的四舍五入,可以使用数学运算
import math
res = list(map(math.ceil, mylist)) # Round up
res = list(map(math.floor, mylist)) # Round down
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.