What's the fastest way to generate delimited string from 1d NumPy array?

Learn about the fastest way to generate delimited string from 1d NumPy array in Python?
Submitted by Pranit Sharma, on March 05, 2023

Generating delimited string from 1d NumPy array

Suppose that we are given a 1D NumPy array of float values and we need to convert these floats into delimited strings.

The separator character which is generally used in a data file for separating the data is called a delimiter. There are some more popular delimiters. E.g.: tab(\t), colon (:), semi-colon (;) etc.

To generate delimited strings from the array, we first need to generate an array with strings and then we will the strings using the str.join() function.

Let us understand with the help of an example,

Python code to generate delimited string from 1d NumPy array

# Import numpy
import numpy as np

# Creating a numpy array
arr = np.random.random(10)

# Display Original array
print("Original array:\n",arr,"\n")

# Creating array of strings
arr_str = np.char.mod('%f', arr)

# Display array of strings
print("Array of strings:\n",arr_str,"\n")

# Joining strings
res = ",".join(arr_str)

# Display result
print("Result:\n",res)

Output:

Example: What's the fastest way to generate delimited string from 1d NumPy array?

Python NumPy Programs »



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.