Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 向列表中所有非整数的元素添加整数_Python_List_Spyder - Fatal编程技术网

Python 向列表中所有非整数的元素添加整数

Python 向列表中所有非整数的元素添加整数,python,list,spyder,Python,List,Spyder,方法1:错误:ufunc“add”不包含签名类型与dtype匹配的循环(' 方法2:错误:必须是str,而不是int x_5 = [x+5 for x in x] 方法3:错误:基数为10的int()的文本无效:'-0.081428368' 我尝试先将x数据转换为整数 x_int = list(map(int, x)) x_5 = [x+5 for x in x] 方法4:错误:“numpy.float64”对象不可编辑 x = numpy.array(x, dtype=float) x

方法1:错误:ufunc“add”不包含签名类型与dtype匹配的循环(' 方法2:错误:必须是str,而不是int

x_5 = [x+5 for x in x]
方法3:错误:基数为10的int()的文本无效:'-0.081428368' 我尝试先将x数据转换为整数

x_int = list(map(int, x))

x_5 = [x+5 for x in x]

方法4:错误:“numpy.float64”对象不可编辑

x = numpy.array(x, dtype=float)
x = numpy.array(x)
x_piu_5= x + 5
x_piu_5=[]
xfl=[float(i) for i in x]
x_piu_5[:] = [x + 5 for x in xfl]
方法5:错误:浮点对象不可编辑

x = numpy.array(x, dtype=float)
x = numpy.array(x)
x_piu_5= x + 5
x_piu_5=[]
xfl=[float(i) for i in x]
x_piu_5[:] = [x + 5 for x in xfl]
大家好

我正在尝试将一个整数添加到我的列表中,该列表包含许多数字,如0.00085695等,我使用了两种方法,但都没有成功

更新1:添加了方法4,我已经获得了我想要的值,但是现在的问题是它说numpy.float是不可编辑的

x = numpy.array(x, dtype=float)
x = numpy.array(x)
x_piu_5= x + 5
x_piu_5=[]
xfl=[float(i) for i in x]
x_piu_5[:] = [x + 5 for x in xfl]

更新2:添加了方法5,我应该在迭代之前将浮点值转换为字符串吗?

问题的核心是列表
x
包含表示浮点数的字符串。您需要将这些字符串转换为
float
对象

更准确地说:

  • 方法1可通过使用
    dtype=float
    进行修复,如注释所示:

    x = numpy.array(x, dtype=float)
    x_5 = x + 5
    
  • 方法2可通过在添加5之前将
    x
    项转换为浮点值来固定:

    x_5 = [float(i) + 5 for i in x]
    
  • 方法3可以通过使用
    float
    而不是
    int
    来修复,因为您的值不是整数而是浮点值:

    x_float = list(map(float, x))
    x_5 = [i + 5 for i in x_float]
    
    请注意,此解决方案相当于方法2,只是在创建附加列表时速度慢了一点,占用了更多的空间

  • 方法4可以通过删除伪
    x=numpy.array(x)
    行来修复。您将得到与方法1相同的代码

  • 至于方法5,我怀疑
    x
    不是通常的列表,而是一个浮点对象


除了将值转换为正确的类型之外,您应该尝试的另一件事是为不同的内容使用不同的变量名。在代码片段中,您对列表/数组和这些列表的元素都使用了
x
。虽然这并非总是严格要求的,但使用不同的变量名可以解决很多问题nfusion,让您免于许多麻烦!

x=numpy.array(x,dtype=float)
Error:'numpy.float64'对象不可编辑您是如何运行第一行代码并在
x
甚至不可编辑的情况下得到不同的错误的?我已经在我使用的每个方法前面提到了我得到的第一个错误,您的方法给了我一个新错误,我分享了这个错误,我不是python专家,也不知道为什么我会列出'x'是或不是iterableWell,你知道什么是
x
吗?你想做什么?这将是一个很好的起点,你也没有提到。非常感谢你,我已经尽了我所能,我只是无法解决问题,我仍然得到一些不合理的反对票,就像websi的成员一样我认为每个人都应该是专家!虽然我看到很短的问题,没有细节,但获得了很高的票数,但我仍然无法消化这一点!如果每个人都是专家,就没有必要举办论坛。现在问题解决了