简体   繁体   English

如何在python中将浮点列表转换为int; err int()参数必须是字符串,类似字节的对象或数字,而不是“列表”

[英]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.

相关问题 int()参数必须是字符串,类似字节的对象或数字,而不是“列表” - int() argument must be a string, a bytes-like object or a number, not 'list' 如何在python中将图像列表转换为数字列表? TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是“ Image” - How to convert list of images into list of numbers in python? TypeError: int() argument must be a string, a bytes-like object or a number, not 'Image' int()参数必须是字符串,类似字节的对象或数字,而不是“列出” Python python-tcod Roguelike - int() argument must be a string, a bytes-like object or a number, not 'list' Python python-tcod Roguelike Python 2-TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是“列表” - Python 2 - TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' 命令行 - int()参数必须是字符串,类似字节的对象或数字,而不是'list' - command line - int() argument must be a string, a bytes-like object or a number, not 'list' int()参数必须是字符串,类似字节的对象或数字,而不是'list'代码错误 - int() argument must be a string, a bytes-like object or a number, not 'list' Error in code Django TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是'list' - Django TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' 错误:装饰器@action中的“int()参数必须是一个字符串,一个类似字节的object或一个数字,而不是'list'” - error: “int() argument must be a string, a bytes-like object or a number, not 'list'” in decorator @action TypeError at /students/exam/1/ int() 参数必须是字符串、类似字节的对象或数字,而不是“列表” - TypeError at /students/exam/1/ int() argument must be a string, a bytes-like object or a number, not 'list' 类型错误:int() 参数必须是字符串、类似字节的对象或数字,而不是“列表”? - TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM