[英]Remove the fixed-size constraint in numpy fixed-size array
我現在有一個固定大小的字符串numpy數組:
import numpy as np
str_arr = np.array(['test1', 'test2'], dtype='<U5')
str_arr[0] = 'longer_string'
print(str_arr)
它返回
['longe' 'test2']
我想取消此限制。 有辦法嗎? 以下是我失敗嘗試的示例:
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')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.