Python:为什么我不能在这种情况下为数组赋新值?(Python: Why can't I assign new value to an array in this case?)

编程入门 行业动态 更新时间:2024-10-17 09:43:03
Python:为什么我不能在这种情况下为数组赋新值?(Python: Why can't I assign new value to an array in this case?) import numpy as np data = np.array([['Height', 'Weight'],['165', '48'],['168', '50'],['173', '53']]) data[0,0] = data[0,0] + "_1"

data [0,0]'Height' ,我想用'Height_1'来代替它。 但上面的代码不起作用。 它返回结果如下:

data[0,0]

'高度'

数据[0,0]元素保持不变。 如果我直接替换而不提及它自己,它仍然不起作用。

data[0,0] = "Height" + "_1"

结果:

data[0,0]

'高度'

但如果我用“高度”以外的其他字符替换它,它就可以工作。

data[0,0] = "str" + "_1"

结果:

data[0,0]

'str_1'

我用这个案例来解释我遇到的问题。 在我的工作中,我必须引用数组本身,因为我需要替换不符合某些要求的元素。 任何人都有解决方案? 谢谢。

import numpy as np data = np.array([['Height', 'Weight'],['165', '48'],['168', '50'],['173', '53']]) data[0,0] = data[0,0] + "_1"

data[0,0] is 'Height', and I want to replace it with 'Height_1'. But the code above doesn't work. It returned the result as:

data[0,0]

'Height'

The data[0,0] element remained the same. And if I replace it directly without referring to itself, it still doesn't work.

data[0,0] = "Height" + "_1"

result:

data[0,0]

'Height'

But if I replace it with some characters other than "Height", it works.

data[0,0] = "str" + "_1"

Result:

data[0,0]

'str_1'

I took this case to explain the problem I'm coming across. And in my work I have to refer to the array itself because I need to replace the elements which don't meet some requirements. Anyone have solutions on this? Thank you.

最满意答案

问题是你的数组是dtype('<U6')

>>> data = np.array([['Height', 'Weight'],['165', '48'],['168', '50'],['173', '53']]) >>> data.dtype dtype('<U6') >>>

它会自动截断:

>>> data[0,0] = "123456789" >>> data array([['123456', 'Weight'], ['165', '48'], ['168', '50'], ['173', '53']], dtype='<U6') >>>

创建阵列时,您始终可以将dtype指定为“对象”,但这会消除numpy开始时的许多速度优势。

或者,您可以指定更长的字符串类型:

>>> data array([['Height', 'Weight'], ['165', '48'], ['168', '50'], ['173', '53']], dtype='<U20') >>> data[0,0]='Height_1' >>> data array([['Height_1', 'Weight'], ['165', '48'], ['168', '50'], ['173', '53']], dtype='<U20') >>>

但要小心,如果你设定的限制太长,你会浪费记忆:

>>> data = np.array([['Height', 'Weight'],['165', '48'],['168', '50'],['173', '53'], ['42','88']], dtype='U20') >>> data.nbytes 800 >>> data = np.array([['Height', 'Weight'],['165', '48'],['168', '50'],['173', '53'], ['42','88']], dtype='U6') >>> data.nbytes 240

如果您只需要有限的字符数量,请考虑使用字节字符串(内存需求的1/4):

>>> data = np.array([['Height', 'Weight'],['165', '48'],['168', '50'],['173', '53'], ['42','88']], dtype='S20') >>> data.nbytes 200 >>>

The problem is your array is of dtype('<U6')

>>> data = np.array([['Height', 'Weight'],['165', '48'],['168', '50'],['173', '53']]) >>> data.dtype dtype('<U6') >>>

It will automatically truncate:

>>> data[0,0] = "123456789" >>> data array([['123456', 'Weight'], ['165', '48'], ['168', '50'], ['173', '53']], dtype='<U6') >>>

You can always specify your dtype as 'object' when you create your array, but this removes a lot of the speed benefits of numpy to begin with.

Alternatively, you can specify a longer string type:

>>> data array([['Height', 'Weight'], ['165', '48'], ['168', '50'], ['173', '53']], dtype='<U20') >>> data[0,0]='Height_1' >>> data array([['Height_1', 'Weight'], ['165', '48'], ['168', '50'], ['173', '53']], dtype='<U20') >>>

But be careful, as if you set the limit too-long you will be wasting memory:

>>> data = np.array([['Height', 'Weight'],['165', '48'],['168', '50'],['173', '53'], ['42','88']], dtype='U20') >>> data.nbytes 800 >>> data = np.array([['Height', 'Weight'],['165', '48'],['168', '50'],['173', '53'], ['42','88']], dtype='U6') >>> data.nbytes 240

If you only need a limited amount of characters, consider using byte-strings (1/4th the memory requirement):

>>> data = np.array([['Height', 'Weight'],['165', '48'],['168', '50'],['173', '53'], ['42','88']], dtype='S20') >>> data.nbytes 200 >>>

更多推荐

本文发布于:2023-08-07 11:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464492.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我不   数组   在这种情况下   Python   case

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!