
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - Ways to convert array of strings to array of floats
String literals in python are surrounded by either single quotation marks, or double quotation marks. Assigning a string to a variable is done with the variable name followed by an equal sign and the string. You can assign a multiline string to a variable by using three quotes.
Example
# array of strings to array of floats using astype import numpy as np # initialising array ini_array = np.array(["1.1", "1.5", "2.7", "8.9"]) # printing initial array print ("initial array", str(ini_array)) # conerting to array of floats # using np.astype res = ini_array.astype(np.float) # printing final result print ("final array", str(res)) # array of strings to array of floats using fromstring import numpy as np # initialising array ini_array = np.array(["1.1", "1.5", "2.7", "8.9"]) # printing initial array print ("initial array", str(ini_array)) # conerting to array of floats # using np.fromstring ini_array = ', '.join(ini_array) ini_array = np.fromstring(ini_array, dtype = np.float, sep =', ' ) # printing final result print ("final array", str(ini_array)) # converting array of strings to array of floats using asarray import numpy as np # initialising array ini_array = np.array(["1.1", "1.5", "2.7", "8.9"]) # printing initial array print ("initial array", str(ini_array)) # conerting to array of floats # using np.asarray final_array = b = np.asarray(ini_array, dtype = np.float64, order ='C') # printing final result print ("final array", str(final_array))
Output
('initial array', "['1.1' '1.5' '2.7' '8.9']") ('final array', '[ 1.1 1.5 2.7 8.9]') ('initial array', "['1.1' '1.5' '2.7' '8.9']") ('final array', '[ 1.1 1.5 2.7 8.9]') ('initial array', "['1.1' '1.5' '2.7' '8.9']") ('final array', '[ 1.1 1.5 2.7 8.9]')
- Related Articles
- How to convert array of comma separated strings to array of objects?
- How to convert array of strings to array of numbers in JavaScript?
- How to convert array of decimal strings to array of integer strings without decimal in JavaScript
- Convert an array of datetimes into an array of strings in Python
- Convert an array of datetimes into an array of strings passing units in Python
- All ways to divide array of strings into parts in JavaScript
- Convert an array of datetimes into an array of strings with UTC timezone in Python
- Convert an array of datetimes into an array of strings passing hour datetime unit in Python
- Convert an array of datetimes into an array of strings passing minutes datetime unit in Python
- Convert an array of datetimes into an array of strings with pytz timezone object in Python
- Convert array of object to array of array in JavaScript
- C Program to Reverse Array of Strings
- C++ Array of Strings
- Convert array of arrays to array of objects grouped together JavaScript
- Program to find number of ways to split array into three subarrays in Python

Advertisements