  # Modify numpy array to store string of arbitrary length

NumPy | Python Methods and Functions | String Variables

Dtype of any numpy array containing string values ​​— it is the maximum length of any string present in the array. Once installed, it will only be able to store a new string, the length of which does not exceed the maximum length at the time of creation. If we try to reassign another string value that is longer than the maximum length of the existing elements, it will simply discard any values ​​that exceed the maximum length.

In this post, we are going to discuss ways to overcome this problem and create a arbitrary length .

Let`s first introduce the problem of creating an empty array of arbitrary length string type.

` `

` # import numpy as np import numpy as np   # Create numy array country = np.array ([ `USA` , `Japan` < code class = "plain">, `UK` ,` ` , ` India `, ` China`])   # Print array print (country) Output: How we can see in the output that the maximum length of any element of the string length in this array is 5. Let`s try to assign a value that is long in the place of the missing value in the array. # Assign" New Zealand "where the missing value is country [ country = = ` `] = ` New Zealand`    # Print the modified array print (country) `

Output: As we can see from the output, "New Z" was assigned and not "New Zealand" due to the length limitation ... Now let`s see how we can overcome this problem.

Challenge # 1: Create an empty array of arbitrary length.

Solution: with create an array, assign it the object type. This allows you to have all the Python string behaviors.

 ` # import numpy library as np ` ` import ` ` numpy as np ` ` `  ` # Create an empty array ` ` # set the object type ` ` country ` ` = ` ` np.array ([` ` `USA` ` `, ` ` `Japan` ` `, ` `` UK` ` `, `` ``, `` ` India ` ``, `` ` China ` ``], dtype = `` ` object ` ` `) `   ` # Print array ` ` print ` ` (country) `

Output: Now we will use an arbitrary length assignment in place of the missing value in this array.

 ` # Assign & # 39; New Zealand & # 39; missing value ` ` country [country ` ` = ` ` = ` ` `` ``] = `` ` New Zealand` `   ` # Print array ` ` print ` ` (country) `

Output: As we can see in the output, we have successfully assigned a string of arbitrary length to this array object.

Problem # 2: Create an empty array of arbitrary length.

Solution: We will use ` numpy.astype () ` to change the dtype of this array object.

` `

 # import numpy library as np ` import ` ` numpy as np ``   # Create an empty array # Please note we have not set the dtype of the object # this will cause a length problem country = np.array ([ ` USA` , `Japan` , ` UK` , ` `, ` India `,` China`])   # Print array print  (country) `

Output: Now we will change the dtype of this array object using the ` numpy function. astype () `. Then we`ll assign it a string of arbitrary length.

 ` # Change the country type ` ` # object & # 39; U256 & # 39; ` ` country ` ` = ` ` country.astype (` ` `U256` ` `) ` ` `  ` # Assign & # 39; New Zealand & # 39; missing value ` ` country [country ` ` = ` ` = ` ` `` ``] = `` ` New Zealand` `   ` # Print array ` ` print ` ` (country) `

Output: As we can see in the output, we have successfully assigned a string of arbitrary length to this array object.

Note . The maximum line length that we can assign in this case after changing the dtype is 256.