Python 删除numpy固定大小数组中的固定大小约束

Python 删除numpy固定大小数组中的固定大小约束,python,arrays,numpy,Python,Arrays,Numpy,我现在有一个固定大小的字符串numpy数组: import numpy as np str_arr = np.array(['test1', 'test2'], dtype='<U5') str_arr[0] = 'longer_string' print(str_arr) 我想取消这个限制。有没有办法做到这一点?下面是我失败尝试的一个示例: str_arr_copy = str_arr.astype(str) str_arr_copy[0] = 'longer_string' pri

我现在有一个固定大小的字符串numpy数组:

import numpy as np

str_arr = np.array(['test1', 'test2'], dtype='<U5')
str_arr[0] = 'longer_string'
print(str_arr)
我想取消这个限制。有没有办法做到这一点?下面是我失败尝试的一个示例:

str_arr_copy = str_arr.astype(str)
str_arr_copy[0] = 'longer_string'
print(str_arr_copy)
这根本没用


谢谢大家!

您可以将其转换为
dtype=object
,进行赋值,然后再转换回
dtype=str

>>> str_arr_copy = str_arr.astype(object)
>>> str_arr_copy[0] = 'longer_string'
>>> print(str_arr_copy.astype(str))
array(['longer_string', 'test2'], 
      dtype='<U13')
>>str\u arr\u copy=str\u arr.astype(对象)
>>>str_arr_copy[0]=“更长的字符串”
>>>打印(str_arr_copy.astype(str))
数组(['longer_string','test2'],

dtype='您可以将其转换为
dtype=object
,进行赋值,然后再转换回
dtype=str

>>> str_arr_copy = str_arr.astype(object)
>>> str_arr_copy[0] = 'longer_string'
>>> print(str_arr_copy.astype(str))
array(['longer_string', 'test2'], 
      dtype='<U13')
>>str\u arr\u copy=str\u arr.astype(对象)
>>>str_arr_copy[0]=“更长的字符串”
>>>打印(str_arr_copy.astype(str))
数组(['longer_string','test2'],
dtype='
str_arr.astype('U100')
为您提供了更多空间,但没有消除限制。对于这项工作,常规列表可能更好。
str_arr.astype('U100')
为您提供了更多空间,但没有消除限制。对于这项工作,常规列表可能更好。使用
arr.tolist()
生成中间副本大约在同一时间运行。使用
arr.tolist()
生成中间副本大约在同一时间运行。