Java map an array of Strings to an array of Integers
Since String
and Integer
are both reference types you can simply call Stream::map
to transform your array.
Integer[] boxed = Stream.of(myarray).map(Integer::valueOf).toArray(Integer[]::new);
Converting string to an int array using map
list(map(int, list(mystr)))
Should do the job. You can't split on an empty string, but Python supports casting str
to list
as a built-in. I wouldn't use map
for situations like these though, but instead use a list comprehension:
[int(x) for x in mystr]
Map a NumPy array of strings to integers
You can use np.unique
with the return_inverse
argument:
>>> lookupTable, indexed_dataSet = np.unique(dataSet, return_inverse=True)
>>> lookupTable
array(['george', 'greg', 'kevin'],
dtype='<U21')
>>> indexed_dataSet
array([2, 1, 0, 2])
If you like, you can reconstruct your original array from these two arrays:
>>> lookupTable[indexed_dataSet]
array(['kevin', 'greg', 'george', 'kevin'],
dtype='<U21')
If you use pandas, lookupTable, indexed_dataSet = pd.factorize(dataSet)
will achieve the same thing (and potentially be more efficient for large arrays).
Convert array of strings to an array of integers
To convert a string to number you have the to_i
method.
To convert an array of strings you need to go through the array items and apply to_i
on them. You can achieve that with map
or map!
methods:
> ["", "22", "14", "18"].map(&:to_i)
# Result: [0, 22, 14, 18]
Since don't want the 0
- just as @Sebastian Palma said in the comment, you will need to use an extra operation to remove the empty strings: (The following is his answer! Vote for his comment instead :D)
> ["", "22", "14", "18"].reject(&:empty?).map(&:to_i)
# Result: [22, 14, 18]
the difference between map
and map!
is that map
will return a new array, while map!
will change the original array.
How to convert a string of numbers to an array of numbers?
My 2 cents for golfers:
b="1,2,3,4".split`,`.map(x=>+x)
backquote is string litteral so we can omit the parenthesis (because of the nature of split function) but it is equivalent to split(',')
. The string is now an array, we just have to map each value with a function returning the integer of the string so x=>+x
(which is even shorter than the Number
function (5 chars instead of 6)) is equivalent to :
function(x){return parseInt(x,10)}// version from techfoobar
(x)=>{return parseInt(x)} // lambda are shorter and parseInt default is 10
(x)=>{return +x} // diff. with parseInt in SO but + is better in this case
x=>+x // no multiple args, just 1 function call
I hope it is a bit more clear.
Javascript - Converting string to number in an array of objects
You are over-complicating things; you just need to map each entry in reports
to its reportId
property and convert that to an integer
let reports = [
{reportId: "21", title: "Online", code: "ON" },
{reportId: "11", title: "Retail", code: "RE" },
{reportId: "61", title: "Walk-in", code: "WI" }
]
let ids = reports.map(function (item) {
return parseInt(item.reportId, 10);
});
console.log(ids)
Related Topics
How to Annotate Ggplot2 Qplot Outside of Legend and Plotarea? (Similar to Mtext())
Create a Concentric Circle Legend for a Ggplot Bubble Chart
Multiplying Vector Combinations
How to Log Transform the Y-Axis of R Geom_Histogram in the Right Direction
Efficient Way to Fill Time-Series Per Group
How to Get This Data Structure in R
Convert Jpg to Greyscale CSV Using R
Drawing Journey Path Using Leaflet in R
Changing the Order of Dodged Bars in Ggplot2 Barplot
R Shiny: Plot with Dynamical Size
Convert Data with One Column and Multiple Rows into Multi Column Multi Row Data
How to Use User Input to Obtain a Data.Frame from My Environment in Shiny