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 240If 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 >>>更多推荐
发布评论